Search code examples
phpzend-framework2

Is there any way to override ZF2 application.config.php directives locally?


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.


Solution

  • 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,
    
        //..
    );