在 bootstrap.php 中使用 zend 自动加载模型
我真的无法弄清楚:
我在 application/models 中创建了一个名为 User.php 的文件。其中的类名为 Model_User。
当我尝试在我的控制器中创建一个对象时,我收到此错误:
Fatal error: Class 'Model_User' not found in C:\xampplite\htdocs\code\application\controllers\IndexController.php on line 14
我用谷歌搜索,发现了这段代码,它应该为我自动加载控制器,它位于 bootstrap.php 中,但代码不起作用。使用此代码的示例正在使用 ZF 1.8,因此这可能是原因,但我无法弄清楚。 我应该如何自动加载我的模型?!
private function _initAutoload(){
$modelLoader = new Zend_Application_Module_Autoloader(array(
'namespace' => '',
'basePath' => APPLICATION_PATH
));
return $modelLoader;
}
有什么想法吗?
I really can't figure this out:
I've created a file called User.php in application/models. The classname in it is Model_User.
When I try to create an object in my Controller, I get this error:
Fatal error: Class 'Model_User' not found in C:\xampplite\htdocs\code\application\controllers\IndexController.php on line 14
I googled around, and found this code, which is supposed to autoload controllers for me, it is located in bootstrap.php The code isn't working though. The example that used this code was working with ZF 1.8 so that might be the reason but I can't figure it out.
How should I autoload my models?!
private function _initAutoload(){
$modelLoader = new Zend_Application_Module_Autoloader(array(
'namespace' => '',
'basePath' => APPLICATION_PATH
));
return $modelLoader;
}
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于我上面链接的问题,答案中的重要一点是命名空间:
namespace
参数告诉自动加载器在遇到以开头的类时在定义的
。第一部分是正确的,但缺少对path
(相对于basePath
)中查找型号_addResourceTypes
的调用。The important bit in the answer to the question I linked above is the namespace:
The
namespace
parameter tells the Autoloader to look in the definedpath
(relative tobasePath
) when encountering a class that starts withModel_
. You have the first part right, but you're missing the call toaddResourceTypes
.