简体   繁体   English

Zend框架模块无法找到/加载模型

[英]Zend Framework Modules can't find/load Models

For some frustrating reason, I configured some Modules, which seemed to work fine, However I cannot load a Modules Models. 由于一些令人沮丧的原因,我配置了一些模块,这些模块似乎工作正常,但是无法加载模块模型。 If I move the Models to the Default they load, but I just can't get the Framework to find them locally.. 如果我将模型移动到默认模型,则会加载它们,但是我无法获取框架以在本地找到它们。

Example: 例:

My Modules Directory is: 我的模块目录是:

application\\modules\\books\\models\\books.php (books is my Model) application \\ modules \\ books \\ models \\ books.php(书是我的模型)

class Application_Module_Books_Model_Books extends Zend_Db_Table_Abstract {}

I also tried.. 我也试过..

Books_Model_Books, Model_Books, books, Modules_.. you name it I tried it :) Books_Model_Books,Model_Books,书籍,Modules_ ..您把它命名为我尝试过的:)

My controller is in the Books Module, and is an Index Controller, and it can never find the Local Model. 我的控制器在“图书”模块中,并且是一个索引控制器,并且它永远找不到本地模型。

I'm using Application.ini and it is configured this way: 我正在使用Application.ini,它是通过以下方式配置的:

resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.params.displayExceptions = 1
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"

I have a BootStrap in the Modules Directory: 我在模块目录中有一个BootStrap:

class Admin_Bootstrap extends Zend_Application_Module_Bootstrap { } Admin_Bootstrap类扩展了Zend_Application_Module_Bootstrap {}

I'm on Zend Framework 1.10, and ideas.. ? 我使用的是Zend Framework 1.10,以及一些想法。

What just fixed it for me was to add the following line to application.ini 刚刚为我解决的问题是将以下行添加到application.ini

resources.modules[]= resources.modules [] =

Then i added a Bootstrap.php to the module directory ('application\\modules\\books\\') 然后我将Bootstrap.php添加到模块目录(“ application \\ modules \\ books \\”)

class Books_Bootstrap extends Zend_Application_Module_Bootstrap {

    protected function _initAutoload()
    {
      $autoloader = new Zend_Application_Module_Autoloader(array(
           'namespace' => 'Books_',
           'basePath' => dirname(__FILE__)
      ));
      return $autoloader;
     }
}    

Move the books model to 'application\\modules\\books\\models\\Books.php' 将书籍模型移至“ application \\ modules \\ books \\ models \\ Books.php”

class Books_Model_Books extends Zend_Db_Table_Abstract {...}

Now you should be able to load the model in the IndexController 现在您应该能够在IndexController中加载模型

$model = new Books_Model_Books();

In the application.ini, add a simple line: 在application.ini中,添加一条简单的行:

resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"

and in the method _initAutoload() inside the Bootstrap put: 并在Bootstrap中的_initAutoload()方法中放置:

$front = $this->bootstrap("frontController")->frontController;
$modules = $front->getControllerDirectory();
$default = $front->getDefaultModule();

foreach (array_keys($modules) as $module) {
    if ($module === $default) {
        continue;
    }

    $moduleloader = new Zend_Application_Module_Autoloader(array(
        'namespace' => $module,
        'basePath'  => $front->getModuleDirectory($module)));
}

make sure the name of models inside each module are like 确保每个模块中的模型名称都与

[name_module]_Model_[name_model]

in you case, like 在你的情况下,就像

class Books_Model_Books {
}

and that's all :D 就是这样:D

The correct class name would be Books_Model_Books, but the filename of that class would need to be Books.php (note the capital 'B'). 正确的类名称为Books_Model_Books,但该类的文件名必须为Books.php(注意大写的“ B”)。

You shouldn't have a bootstrap in the modules directory, but you probably do want a bootstrap for each module directory, so you'd need a class: 您不应该在modules目录中有一个引导程序,但是您可能确实希望每个模块目录都有一个引导程序,因此您需要一个类:

class Books_Bootstrap extends Zend_Application_Module_Bootstrap
{

}

at: application/modules/books/Bootstrap.php (again note the capital 'B'). 网址:application / modules / books / Bootstrap.php(再次注意大写的“ B”)。

Check the section on the module resource autloader at http://framework.zend.com/manual/en/zend.loader.autoloader-resource.html for more info. 有关更多信息,请查看http://framework.zend.com/manual/en/zend.loader.autoloader-resource.html上有关模块资源自动加载程序的部分。

1-feel free to delete this cuz you don't need it any more : 1-随意删除此cuz,您不再需要它:

resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"

and place this code inside you Bootstrap.php file [application bootstrap ] not the module bootstrap 并将此代码放入您的Bootstrap.php文件[application bootstrap]而不是模块bootstrap中

public function _initAutoload()
{
$autoloader = new Zend_Application_Module_Autoloader(array(
'namespace' => '' , 
'basePath' => dirname(__FILE__) . '/modules/'));
return $autoloader;
}

getting back to config you need also to add 回到配置,您还需要添加

   resources.modules[] = ""    
   resources.frontController.defaultModule = "admin" 

here is my complete config file : 这是我完整的配置文件:

phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0
includePaths.library = APPLICATION_PATH "/../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
appnamespace = "Application"
resources.frontController.defaultModule = "news"
resources.frontController.prefixDefaultModule = 1
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
resources.modules[] = ""
;resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.params.displayExceptions = 1
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"
autoloaderNamespaces[] = "xxxxxx"

resources.db.adapter = "Mysqli"
resources.db.isdefaulttableadapter = true
resources.db.params.host = "localhost"
resources.db.params.dbname = "xxxxx"
resources.db.params.username = "root"
resources.db.params.password = "root"
resources.db.params.charset = "utf8"

hopefully i didn't miss any thing 希望我没有错过任何事情

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM