Zend Framework Modules can't find/load Models

2020-06-06 06:24发布

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)

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 :)

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:

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:

class Admin_Bootstrap extends Zend_Application_Module_Bootstrap { }

I'm on Zend Framework 1.10, and ideas.. ?

4条回答
Lonely孤独者°
2楼-- · 2020-06-06 06:55

What just fixed it for me was to add the following line to application.ini

resources.modules[]=

Then i added a Bootstrap.php to the module directory ('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'

class Books_Model_Books extends Zend_Db_Table_Abstract {...}

Now you should be able to load the model in the IndexController

$model = new Books_Model_Books();
查看更多
Animai°情兽
3楼-- · 2020-06-06 06:57

In the application.ini, add a simple line:

resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"

and in the method _initAutoload() inside the Bootstrap put:

$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

查看更多
放荡不羁爱自由
4楼-- · 2020-06-06 07:01

1-feel free to delete this cuz you don't need it any more :

resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"

and place this code inside you Bootstrap.php file [application bootstrap ] not the module 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

查看更多
Evening l夕情丶
5楼-- · 2020-06-06 07:18

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').

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:

class Books_Bootstrap extends Zend_Application_Module_Bootstrap
{

}

at: application/modules/books/Bootstrap.php (again note the capital 'B').

Check the section on the module resource autloader at http://framework.zend.com/manual/en/zend.loader.autoloader-resource.html for more info.

查看更多
登录 后发表回答