在 bootstrap.php 中使用 zend 自动加载模型

发布于 2024-10-07 19:17:46 字数 632 浏览 0 评论 0原文

我真的无法弄清楚:

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

单挑你×的.吻 2024-10-14 19:17:46

对于我上面链接的问题,答案中的重要一点是命名空间:

    $resourceLoader->addResourceTypes(array(
            'model' => array(
                    'namespace' => 'Model',
                    'path'      => 'models'
            )
    ));

namespace 参数告诉自动加载器在遇到以 开头的类时在定义的path(相对于basePath)中查找型号_。第一部分是正确的,但缺少对 addResourceTypes 的调用。

The important bit in the answer to the question I linked above is the namespace:

    $resourceLoader->addResourceTypes(array(
            'model' => array(
                    'namespace' => 'Model',
                    'path'      => 'models'
            )
    ));

The namespace parameter tells the Autoloader to look in the defined path (relative to basePath) when encountering a class that starts with Model_. You have the first part right, but you're missing the call to addResourceTypes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文