简体   繁体   English

如何使Zend Framework使用“特定于模块”的配置?

[英]How do I get Zend Framework to use 'module specific' configurations?

Reading the section Zend_Application_Resource_Modules in the docs here: http://framework.zend.com/manual/1.10/en/zend.application.available-resources.html 在此处阅读文档中的Zend_Application_Resource_Modules部分: http ://framework.zend.com/manual/1.10/en/zend.application.available-resources.html

I noticed this: 我注意到了这一点:

You can specify module-specific configuration using the module name as a prefix or sub-section in your configuration file. 您可以使用模块名称作为配置文件中的前缀或子部分来指定特定于模块的配置。

using this: 使用这个:

  [production]
  news.resources.db.adapter = "pdo_mysql"
  news.resources.db.params.host = "localhost"
  news.resources.db.params.username = "webuser"
  news.resources.db.params.password = "XXXXXXX"
  news.resources.db.params.dbname = "news"

To me this is a good idea. 对我来说,这是一个好主意。 But, when I simply add these prefixes to certain things I want to be specific to my modules, nothing changes. 但是,当我只是将这些前缀添加到某些我想特定于我的模块的东西时,没有任何变化。

So my question is: How do I tell Zend Framework to actually use these module specific prefixes? 所以我的问题是:如何告诉Zend Framework实际使用这些模块特定的前缀?

I use the following implementation of modules in Zend. 我在Zend中使用以下模块实现。 It allows you to use "module-specific" configuration. 它允许您使用“特定于模块”的配置。

application/config/config.ini
-----------------------------
[production]
resources.modules[] =

By doing this, you're telling Zend_Application that you want to use the Modules Bootstrap Resource plugin. 这样,您就告诉Zend_Application您要使用Modules Bootstrap Resource插件。 The Modules plugin will load a separate bootsrap class for each of your modules, excluding the default module. Modules插件将为每个模块(默认模块除外)加载一个单独的bootsrap类。 Therefore, you need to create a new bootstrap class for your second module. 因此,您需要为第二个模块创建一个新的引导类。

application/modules/news/Bootstrap.php
-----------------------------
class News_Bootstrap extends Zend_Application_Module_Bootstrap {

   //---------------------------------------
   // Automatically load our resources
   //
   // NOTE: You don't have to add this, its
   //       just and example to show that you
   //       can customize the bootstrap
   //       process just for this module.
   public function _initModuleResourceAutoloader(){

      $this->getResourceLoader()->addResourceTypes(array(
         'modelResource' => array(
            'path' => 'models/resources',
            'namespace' => 'Resource'
         )
      ));
   }
}

This "News_Bootstrap" class will now be loaded and executed during the bootstrap process. 现在将在引导过程中加载并执行“ News_Bootstrap”类。

The naming convention for this file is important as the Modules Resource plugin needs to be able to find the class. 该文件的命名约定很重要,因为Modules Resource插件需要能够找到该类。 Note that you must name the file Bootstrap.php. 请注意,您必须将文件命名为Bootstrap.php。

Finally, you'll notice that you're subclassing the Zend_Application_Module_Bootstrap rather than Zend_Application_Bootstrap_Bootstrap like you do in the main bootstrap. 最后,您会注意到您正在对Zend_Application_Module_Bootstrap进行子类化,而不是像在主引导程序中那样对Zend_Application_Bootstrap_Bootstrap进行子类化。

Now, your module-specific configuration should work: 现在,特定于模块的配置应该可以工作了:

[production]
news.resources.db.adapter = "pdo_mysql"
news.resources.db.params.host = "localhost"
news.resources.db.params.username = "webuser"
news.resources.db.params.password = "XXXXXXX"
news.resources.db.params.dbname = "news"

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

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