Separate routes file

Might be a noob question, but I’m wondering what would be the best way of isolating the routes to a single file.

I’m currently using dev, staging, and production config files to manage different db options and would like to use one file for my routes to make the project more maintainable.

Example: I would like to have everything in the ‘rules’ array isolated.




'urlManager'=>array(

	'urlFormat'=>'path',

	'showScriptName'=>false,

	'rules'=>array(

		'xxx' => 'xxx/xxx',

		'xxx = > ' xxx/xxx',

		),

	),



You may use one main config file and to use it in your dev and staging versions, overwriting only changed components (e.g. ‘db’ component) configs, thus you’ll have rules in main config file only.

Example, your dev.php





return CMap::mergeArray(

                require(dirname(__FILE__) . '/main.php'), array(

            'modules' => array(

                'gii' => array(

                    'class' => 'system.gii.GiiModule',

                    'password' => 'admin',

                ),

            ),

            'components' => array(

                'cache' => array(

                    'class' => 'system.caching.CDummyCache',

                ),

                'db' => array(

                    'class'=>'DbConnection',

                    'connectionString' => 'mysql:host=127.0.0.1;dbname=LOCALDB’,

                    'emulatePrepare' => true,

                    'username' => '',

                    'password' => '',

                    'charset' => 'utf8',

                    'enableProfiling' => true,

                    'enableParamLogging' => true,

                ),

                'errorHandler' => array(

                    'errorAction' => null,//'site/error',

                ),

            ),

            'params' => array(

            ),

                )

);



On more convenient thing is about different console config files, not to duplicate things:

Example, your console_dev.php





$mainConfig = require(dirname(__FILE__) . '/dev.php');

return array(

	'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',

	'name'=>'My Console Application',

        'import' => $mainConfig['import'],

        'modules' => $mainConfig['modules'],

	'components'=>array(

                'request' => array(

                    'hostInfo' => 'http://domain',

                    'baseUrl' => '',

                    'scriptUrl' => '',

                ),

            'settings' => $mainConfig['components']['settings'],

            'db' => $mainConfig['components']['db'],

            'format' => $mainConfig['components']['format'],

            ),

	),

);



Hope it’ll help or at least will give you fresh ideas.

Very cool solution, thanks. I ended up with the structure in your example and it works great (main.php is production, and then dev.php and stage.php overwrite or add some parameters). Not use to php, but I guess $var = (file with return statement in it) being a kind of standard way of isolating variables?

Nice to hear the solution worked for you.

I’m not sure if I’ve got your question correctly, though. Yes, $var stores any assigned value.

You may read more about variables at php docs