Home  >  Article  >  php教程  >  zendframework initialization configuration

zendframework initialization configuration

WBOY
WBOYOriginal
2016-08-18 08:57:551122browse

https://framework.zend.com/manual/2.4/en/tutorials/config.advanced.html#environment-specific-system-configuration

There are two levels of configuration: global and local, that is, system configuration and application configuration.

System configuration: System configuration is used to pass to the Application instance. The Application instance uses these contents to locate the ModuleManager and ServiceManager.

Application configuration: ModuleManager will use ConfigListener to merge the configuration of each module when loading the module. These configurations are called application configurations. The configuration of each module will eventually be merged with the configuration file under config/autoload/.

 The application configuration will be passed to the EVENT_MERGE_CONFIG event before being passed to the ServiceManager, which will allow additional modifications in the future


System Configuration:

 Before loading the module, we must tell the Application instance: what modules there are and where they are.

Fields included in the system configuration:

<span style="color: #008000;">//</span><span style="color: #008000;">包含整个应用中用到的模块,一般是模块的命名空间。</span>
'modules' =><span style="color: #000000;"> [
    </span>'Application',<span style="color: #000000;">
]</span>,

<span style="color: #008000;">//</span><span style="color: #008000;">module_listener_options留给ModuleManager的监听器使用(Zend\ModuleManager\Listener\ConfigListener</span>
'module_listener_options' =><span style="color: #000000;"> [

    </span><span style="color: #008000;">//</span><span style="color: #008000;">指明了模块的存储位置,一般在/module和/vendor两个目录下。</span>
    'module_paths' =><span style="color: #000000;"> [
        </span>'./module',
        './vendor',
    <span style="color: #008000;">//</span><span style="color: #008000;">也可以使用string key  </span>
        'module_namespace' => 'path_to_the_module's_Module_Class'<span style="color: #000000;">
    ],

    //模块加载之后的全局配置文件的路径。可以使用GLOB_BRACE标记:http://cn2.php.net/glob
    </span>'config_glob_paths'<span style="color: #000000;"> => [
        </span>'config/autoload/{{,*.}<span style="color: #0000ff;">global</span>,{,*.local}.php'<span style="color: #000000;">,
    ],

    //是否使用configuration cache。如果使用配置将会被缓存用于后续请求
//    </span>'config_cache_enabled'<span style="color: #000000;"> => $booleanValue,
    //创建配置缓存文件的名字
//    </span>'config_cache_key'<span style="color: #000000;"> => $stringKey,
    
    //是否使用模块类映射缓存。
//    </span>'module_map_cache_enabled'<span style="color: #000000;"> => $booleanValue,
    //缓存文件名
//    </span>'module_map_cache_key'<span style="color: #000000;"> => $stringKey,

    //缓存文件的路径
    //</span>'cache_dir'<span style="color: #000000;"> => $stringPath,

    //是否检查模块之间的依赖,默认检查。如果某个模块的抵赖模块没有加载,那这个模块也不会使用
    //</span>'check_dependencies'<span style="color: #000000;"> => true,
], //以上为</span>'<span style="color: #000000;">module_listener_options内容。

</span><span style="color: #008000;">//</span><span style="color: #008000;">用来创建自己的service manager
//'service_listener_options' => [
//    [
//        'service_manager' => $stringServiceManagerName,
//        'config_key'           => $stringConfigKey,
//        'interface'              => $stringOptionalInterface,
//        'method'                => $stringRequiredMethodName,
//    ],</span>
],

<span style="color: #008000;">//</span><span style="color: #008000;">用来初始化ServiceManager的初始配置。
//必须和Zend\ServiceManager\Config兼容
//'service_manager' => [],</span>

The annotated parts are optional. System configuration is loaded before the application starts, so it is generally very small. Except service_manager can be overridden in the module configuration file, the rest are not overridable.

Select the configuration file according to the application scenario:

  Sometimes we want to use one configuration in development mode and another configuration in production environment. We can add the following instructions in apache.conf or .htaccess:

SetEnv "APP_ENV" "development"

 Use getenv() or $_SERVER[] in PHP to obtain the server environment variables, and then set the configuration according to the environment variables.

'config_glob_paths' =><span style="color: #000000;"> [
    </span><span style="color: #008080;">sprintf</span>('config/autoload/{,*.}{global,%s,local}.php', <span style="color: #800080;">$env</span><span style="color: #000000;">)
]</span>

Module configuration:

 Each module can provide its own configuration file.

Use getConfig() to return the module's own configuration. This method will be automatically called when moduleManager loads the module.

<span style="color: #008000;">//</span><span style="color: #008000;">File:module.php</span>

<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> getConfig()
{
    </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">include</span> __DIR__ . '/config/module.php'<span style="color: #000000;">;
}</span>

getConfig provides configuration for all available Manager classes provided by ServiceManager (such as: ControllerManager...).

 If you want to target a certain manager class, you can use the corresponding module method, such as: getControllerConfig() and so on. https://framework.zend.com/manual/2.4/en/tutorials/config.advanced.html#configuration-mapping-table

 

Priority of configuration information:

Merging order of various configurations:

 1. Various service configuration methods in the module class

 2. The configuration returned by getConfig() will cover other service configuration methods. Note: The configuration returned by this method will not be cached (so it is best to use different service configuration methods).

Operation merge configuration information:

  ZendModuleManagerListenerConfigListener will trigger the ZendModuleManagerModuleEvent::EVENT_MERGE_CONFIG event before merging all configurations but not passing them to ServiceManager. By listening to this event you can operate on the merged configuration.

<?<span style="color: #000000;">php
namespace FOO;
</span><span style="color: #0000ff;">use</span><span style="color: #000000;"> Zend\ModuleManager\ModuleEvent;
</span><span style="color: #0000ff;">use</span><span style="color: #000000;"> Zend\ModuleManager\ModuleManager;

</span><span style="color: #0000ff;">class</span><span style="color: #000000;"> Module
{
    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> init(ModuleManager <span style="color: #800080;">$moduleManager</span><span style="color: #000000;">)
    {
        </span><span style="color: #800080;">$events</span> = <span style="color: #800080;">$moduleManager</span>-><span style="color: #000000;">getEventManager();
        </span><span style="color: #800080;">$events</span>->attach(ModuleEvent::EVENT_MERGE_CONFIG, <span style="color: #0000ff;">array</span>(<span style="color: #800080;">$this</span>, 'onMergeConfig'<span style="color: #000000;">));
    }
    
    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> onMergeConfig(ModuleEvent <span style="color: #800080;">$e</span><span style="color: #000000;">)
    {
        </span><span style="color: #800080;">$configListener</span> = <span style="color: #800080;">$e</span>-><span style="color: #000000;">getConfigListener();
        </span><span style="color: #800080;">$config</span> = <span style="color: #800080;">$configListener</span>->getMergedConfig(<span style="color: #0000ff;">false</span><span style="color: #000000;">);
        
        </span><span style="color: #0000ff;">if</span> (<span style="color: #0000ff;">isset</span>(<span style="color: #800080;">$config</span>['some_key'<span style="color: #000000;">])) {     
            </span><span style="color: #0000ff;">unset</span>(<span style="color: #800080;">$config</span>['some_key'<span style="color: #000000;">]);
        }
        
        </span><span style="color: #800080;">$configListener</span>->setMergedConfig(<span style="color: #800080;">$config</span><span style="color: #000000;">);
    }
}</span>

Workflow for merging configuration information:

System configuration:

 Defined in config/application.config.php;

 It will not be merged;

 Allows programmatic control configuration.

 Configuration information is passed to the Application instance. ModuleManager initializes the system sequentially.

Application configuration:

ModuleManager merges each module class defined in the system configuration in the following order:

  Service configuration defined in Module class method

  Configuration returned by Module::getConfig()

 File settings defined by config_glob_paths in service configuration

 EVENT_MERGE_CONFIG event triggered by ConfigListener: Why does ConfigListener need to be configured? Other listeners control the configuration (modification)

 The final merged configuration is passed to ServiceManager.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn