Zendframework Initialization Configuration and zendframework Configuration

Source: Internet
Author: User
Tags autoload

Zendframework Initialization Configuration and zendframework Configuration

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: the system configuration is passed to the Application instance. The Application instance uses this content to locate ModuleManager and ServiceManager.

Application configuration: ModuleManager merges the configuration of each module with ConfigListener when loading the module. These configurations are called Application configurations. The configuration of each module is eventually combined with the configuration file under config/autoload.

The application configuration will be passed to the EVENT_MERGE_CONFIG event before it is passed to ServiceManager. This will allow additional modifications in the future.

System Configuration:

Before loading a module, we must tell the Application instance which modules are available and where these modules are located.

Fields contained in the System Configuration:

// Contains the modules used in the entire application, generally the module namespace. 'Modules' => ['application',], // le_listener_options is left for the ModuleManager Listener (Zend \ ModuleManager \ Listener \ ConfigListener 'module _ listener_options '=> [// specifies the storage location of the module, it is usually in the/module and/vendor directories. 'Module _ paths '=> ['. /module ','. /vendor', // you can also use string key'module _ namespace '=> 'path _ to_the_module's _ Module_Class'], // path of the global configuration file after the module is loaded. You can use GLOB_BRACE flag: http://cn2.php.net/glob 'config _ glob_paths '=> ['config/autoload /{{,*.} global ,{,*. local }. php ',], // whether to use configuration cache. If the configuration is used, it will be cached for subsequent requests // 'config _ cache_enabled '=> $ booleanValue, // create the name of the configuration cache file // 'config _ cache_key '=> $ stringKey, // whether to use the module class ing cache. // 'Module _ map_cache_enabled '=> $ booleanValue, // cache file name // 'module _ map_cache_key' => $ stringKey, // cache file path // 'cache _ dir' => $ stringPath, // whether to check the dependencies between modules. The default check is used. If the denial module of a module is not loaded, the module will not use // 'check _ Dependencies' => true,], // The above content is 'module _ listener_options. // Create your own service manager // 'service _ listener_options '=> [// [// 'service _ manager' => $ stringServiceManagerName, // 'config _ key' => $ stringConfigKey, // 'interface' => $ stringOptionalInterface, // 'method' => $ stringRequiredMethodName, //],], // initialize the initial configuration of ServiceManager. // It must be compatible with Zend \ ServiceManager \ Config // 'service _ manager' => [],

The comments are optional. The system configuration is loaded before the application starts, so it is usually very small. In addition to service_manager, the module configuration file can be reloaded, And the rest cannot be overwritten.

 

Select a configuration file based on the Application Scenario:

Sometimes we want to use one configuration in the Development Mode and another configuration in the formal environment. You can add the following commands 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' => [    sprintf('config/autoload/{,*.}{global,%s,local}.php', $env)]

 

 

Module Configuration:

Each module can provide its own configuration file.

Use getConfig () to return the module configuration. This method is automatically called when moduleManager loads the module.

//File:module.phppublic function getConfig(){    return include __DIR__ . '/config/module.php';}

GetConfig provides available Manager classes (for example, ContorllerManager) for all ServiceManager services ..) Provides configuration.

You can use the corresponding module methods for a manager class, such as getControllerConfig. 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 overwrite other service configuration methods. Note: The configurations returned by this method are not cached (so it is best to use different service configuration methods ).

 

Configuration information for merging operations:

Before merging all configurations but not passing them to ServiceManager, Zend \ ModuleManager \ Listener \ ConfigListener triggers the Zend \ ModuleManager \ ModuleEvent: EVENT_MERGE_CONFIG event. By listening to this event, you can perform operations on the merged configurations.

<?phpnamespace FOO;use Zend\ModuleManager\ModuleEvent;use Zend\ModuleManager\ModuleManager;class Module{    public function init(ModuleManager $moduleManager)    {        $events = $moduleManager->getEventManager();        $events->attach(ModuleEvent::EVENT_MERGE_CONFIG, array($this, 'onMergeConfig'));    }        public function onMergeConfig(ModuleEvent $e)    {        $configListener = $e->getConfigListener();        $config = $configListener->getMergedConfig(false);                if (isset($config['some_key'])) {                 unset($config['some_key']);        }                $configListener->setMergedConfig($config);    }}

 

 

 

Workflow for merging configuration information:

System Configuration:

Defined in config/application. config. php;

Not merged;

Allow programmatic configuration control.

The configuration information is passed to the Application instance. ModuleManager initializes the system in sequence.

Application Configuration:

ModuleManager merges the module classes defined in the System Configuration in the following order:

Service configuration defined in the Module class Method

Module: configuration returned by getConfig ()

File Settings defined by config_glob_paths in service configuration

EVENT_MERGE_CONFIG event triggered by ConfigListener: Why is ConfigListener configured? Other listeners control the configuration (modify)

The merged configuration is passed to ServiceManager.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.