I'm working on a ZF2 project and my public/index.php file is like follows:
<?php
chdir(dirname(__DIR__));
require 'init_autoloader.php';
Zend\Mvc\Application::init(require 'config/application.config.php')->run();
Application initialize process starts with using application.config.php and I know that ZF2 provides nice way to override module configurations locally via filenames like modulename.local.php but not for application.config.php file.
For example, in application.config.php, I have a module_listener_options key as follows:
return array(
'modules' => array(
// ...
),
'module_listener_options' => array(
'module_paths' => array(
// ...
),
'config_glob_paths' => array(
'config/autoload/{,*.}{global,local}.php',
),
'config_cache_enabled' => TRUE,
'config_cache_key' => 'configuration_cache',
'cache_dir' => __DIR__ . '/../data/cache'
// ...
)
So I want to disable config caching locally while working on a development environment but I want to turn it on in a production environment without need a post-deployment tricks (like writing custom git-hook / bash script etc..).
Also, I have a APPLICATION_ENVIRONMENT
$_ENV
variable on all servers (dev, prod, test) I don't know what is the best approach to achieve this in ZF2.
I found the Stephen Rees-Carter's article, yes workarounds this problem but i want to learn is there any other / more elegant solutions which doesn't depends on composer.
You could just test your environment variable in your app config and set caching accordingly, eg.,
<?php
// application.config.php
$env = getenv('APPLICATION_ENVIRONMENT');
$configCacheEnabled = ($env == 'production');
return array(
//..
'config_cache_enabled' => $configCacheEnabled,
//..
);
Here's an example to include modules only on your development setup, with a local file override. You can easily just remove the environment variable check if you wished to just override with the presence of the local config file.
application.config.php
$config = array(
'modules' => array(
'Application',
'ZfcBase',
),
'module_listener_options' => array(
'config_glob_paths' => array(
'config/autoload/{,*.}{global,local}.php',
),
'module_paths' => array(
'./module',
'./vendor',
),
),
);
if(getenv('APPLICATION_ENV') == 'development' && is_readable('config/autoload/application.config.local.php')){
$localAppConfig = require 'config/autoload/application.config.local.php';
$config = array_merge_recursive($config,$localAppConfig);
}
return $config;
config/application.config.local.php
return array(
'modules' => array(
'ZendDeveloperTools',
'ZFTool'
),
/**
* Add any overrides to the new local config
*/
);
You can then just add overrides to your local file, which can be different for staging and production environments.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With