ZF2 – Split Module Configuration in Several Files

zend-logo

It is common situation to have too many configuration options in a single ZF2 module config file so it will be not so easy to manage all of them. In this situation the best solution is to split config options in multiple files. Here is how you can achieve this.

Let’s say that we want to move routes config into separate file

1) Create file /config/module.config.routes.php and add the following content:

<?php
return array(
    'router' => array(
        'routes' => array(
            // Your routes here
        ),
    ),
);

2) Change the method getConfig() in Module.php file in order to merge all config arrays, for example:

public function getConfig()
{
    $config = array();

    $configFiles = array(
        __DIR__ . '/config/module.config.php',
        __DIR__ . '/config/module.config.routes.php', // This is your new config file
    );

    // Merge all module config options
    foreach($configFiles as $configFile) {
        $config = \Zend\Stdlib\ArrayUtils::merge($config, include $configFile);
    }

    return $config;
}

 

Share This Post

Related Articles

Leave a Reply

 
© 2014 LoneShooter.com. All rights reserved. Site Admin · Entries RSS · Comments RSS
· Designed by Lone Shooter