configuration - How to use the global.php/local.php configs in the getConfig() of a module in a Zend Framework 2 application? -
in zf2 application have cofigs, that: 1. need different dependening on environment; 2. specific concrete module. i'm curently using here described:
global.php & local.php
return array( ... 'modules' => array( 'cache' => array( 'ttl' => 1, // 1 second ) ) ... );
module class
module { ... public function getserviceconfig() { try { return array ( 'factories' => array( 'zend\cache\adapter\memcachedoptions' => function ($servicemanager) { return new memcachedoptions(array( 'ttl' => $this->getconfig()['modules']['cache']['ttl'], ... )); }, ... ) ); } ... } ... }
it's working fine, believe, module specific settings should accessed on 1 central place in module -- getconfig()
method of module
class. this:
class module { public function getconfig() { $moduleconfig = include __dir__ . '/config/module.config.php'; $application = $this->getapplicationsomehow(); // <-- how? $applicationmoduleconfig = $application->getconfig()['modules'][__namespace__]; $config = array_merge($moduleconfig, $applicationmoduleconfig); return $config; } ... public function getserviceconfig() { try { return array ( 'factories' => array( 'zend\cache\adapter\memcachedoptions' => function ($servicemanager) { return new memcachedoptions(array( 'ttl' => $servicemanager->get('config')['modules']['cache']['ttl'], ... )); }, ... ) ); } ... } ... }
the problem is, don't get, how access global.php/local.php configs in getconfig()
of module. how can it?
every single configuration of every single loaded module merged 1 single config. namely be:
$servicemanager->get('config');
the reason behind (global|local).config.php
merely usage purpose. global configuration files should deployed. local configuration files should deployed distributionables, alias local.config.php.dist
.
distributionals not loaded, no matter places. common notion of zf2 copy distributionables /config/autoload
-directory of zf2 application , rename them local.config.php
one example:
// yourmodule/config/module.config.php return array( 'key' => 1337 ); // yourmodule/config/local.yourmodule.php.dist return array( 'key' => 7331 );
now when publish / deploy application, module.config.php
used. if wants change configuration of module, never touch module.config.php
, file overwritten when module updated.
however people can copy:
yourmodule/config/local.yourmodule.php.dist /config/autoload/local.yourmodule.php
and change config values inside local configuration.
to understand:
- you should configure module best possible live-scenario.
- if have environment-specific needs, overwrite config using local-config
- local configs never deployed automatically, manual task needing done inside environment itself
hope got little more clear
ultimately:
- configure module live-scenario
- on development machine create
/config/autoload/mymodule.local.php
, overwritettl
it's development value
loadorder:
the last interesting part, have forgotten about, load order of configuration files. files merged, important note!
- first load
/config/application.config.php
- second load each modules
/modules/{module}/config/module.config.php
* - last not least autoloadable files loaded
/config/autoload/{filename}.php
asterix not module.config.php
called, rather module
-classes configuration functions. these are:
getconfig()
getserviceconfig()
getviewhelperconfig()
- ultimately under
zend\modulemanager\feature\{feature}providerinterface
if understand this part of configlistener
correctly, getconfig()
called first , of specialiced {feature}providerinterfaces
overwrite data of getconfig()
, don't take granted, need check!
Comments
Post a Comment