2009-10-06 114 views
6

有没有一种方法可以为每个模块建立模型?我有3个模块,其中一个是“contacts”模块。 我创建了它模块/联系人/模型/模型Codes.php 代码控制器Zend Framework模块化应用程序,无法为每个模块加载模型,自动加载模型?

class Contacts_CodesController extends Zend_Controller_Action 
{ 

    public function init() 
    { 
     /* Initialize action controller here */ 
    $this->view->messages = $this->_helper->flashMessenger->getMessages(); 

    } 

    public function indexAction() 
    { 

    $codesTable = new Contacts_Model_Codes(); 

    } 

代码型号:

class Contacts_Model_Codes extends Zend_Db_Table 
{ 
    protected $_name = 'codes'; 
} 

的错误,我得到: 致命错误:类'Contacts_Model_Codes'找不到/Applications/MAMP/htdocs/zf_site/application/modules/contacts/controllers/CodesController.php 26行

感谢

回答

5

您必须先注册与自动加载器“Contacts_”命名空间。你可以为此使用Zend_Application_Module_Autoloader。

$autoloader = new Zend_Application_Module_Autoloader(array(
     'namespace' => 'Contacts_', 
     'basePath' => dirname(__FILE__) . '/modules/cotacts', 
    )); 

这将在您提供的basePath内为您的模块创建以下映射。

api/   => Api 
forms/  => Form 
models/  => Model 
    DbTable/ => Model_DbTable 
plugins/  => Plugin 

如果使用Zend_Application到自举你的应用程序,你不应该需要这个它”模块,因为文档说:

When using module bootstraps with Zend_Application, an instance of Zend_Application_Module_Autoloader will be created by default for each discrete module, allowing you to autoload module resources.

+1

这仅仅是ZF 1.8属实,在这个版本中添加的模块自动加载和Zend_Application用它来引导模块。 在旧版本中,您将需要使用Zend_Application_Resource_Modules – NDM 2009-10-06 10:58:29

2

我使用的是1.9版本。 这是我举的一部分:

protected function _initAutoload() { 
    $modelLoader = new Zend_Application_Module_Autoloader(array(
        'namespace' => '', 
        'basePath' => APPLICATION_PATH.'/modules/default') 

     ); 
} 

    protected function _initAutoloaders() 
    { 
     $this->getApplication()->setAutoloaderNamespaces(array('Eric_')); 
     return $this; 
    } 

    protected function _initPlugins() 
    { 
     $this->bootstrap('autoloaders'); 
     $this->bootstrap('frontController'); 

     // register the plugin for setting layouts per module 
     $plugin = new Eric_Plugin_Modularlayout(); 
     $this->frontController->registerPlugin($plugin); 
      return $modelLoader; 
    } 

插件Eric_Plugin_Modularlayout设定每个模块的正确布局。

我有3个模块:default,ez,contacts。 有趣的是,在联系行为中,我可以在ez/models目录中调用模型。没有问题。

18

我发现了这个问题。我忘了将自举文件放入我的联系人模块中。 现在一切正常,我可以让我的模块使用他们自己的模型。

class Contacts_Bootstrap extends Zend_Application_Module_Bootstrap 
{ 

} 
+5

同时将“resources.modules [] =”添加到您的application.ini – 2011-06-30 15:36:46

+0

谢谢,这对我有所帮助。我想知道为什么这两件事都没有被“zf create module x”所关注。有没有你不想要他们的情况? – LinusR 2015-04-29 13:49:33

5

添加

resources.modules[] = 

要你的配置INI

6

我已经找到了解决办法,我猜! :) 这是一个问题,当你在的application.ini文件中添加下一个资源

resources.frontController.defaultModule = "Default" 

,你也使用某种参数。我认为这是一个错误。

实现模块正确的方法是:

1 - 创建所需的模块和 '默认' 模块与ZF工具

2 - 在apllication.ini告诉ZF其中模块是和其中那些模块的控制器,以

resources.frontController.moduleDirectory = APPLICATION_PATH "/modules" 
resources.frontController.moduleControllerDirectoryName = "controllers" 

使用已知

resources.modules = "" 

并设置:

resources.frontController.params.prefixDefaultModule = "" 

这一点很重要,因为ZF工具将它设置为 “1”。这是错误。 :)

并记住不要错过默认模块!

3 - 为每个模块创建引导文件,并把:

如果我的模块是 'Evacol':

<?php 
class Evacol_Bootstrap extends Zend_Application_Module_Bootstrap 
{ 
} 

保存到/模块/ Evacol/明显

注意Evacol_ ...和..._ Module_Bootstr ...我的模块的名称扩展了正确的类。 请勿使用使用zf工具创建的引导程序文件的默认值。我做到了:)

不要修改任何其他的东西。这并不是必要的。

瞧!相信我。有用!

这是Zend框架1.10.8