Self-referencing config

If a slight loss of performance is acceptable, this might be an useful solution for accessing e.g basePath and params from inside the config array

main.php




<?php

// uncomment the following to define a path alias

// Yii::setPathOfAlias('local','path/to/local-folder');


$preConfig = array(

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

  'params'=>array(

    'nameSuffix'=>'(demo)',  // example

    ...

    ...

  ),

);


// This is the main Web application configuration. Any writable

// CWebApplication properties can be configured here.


//return CMap::mergeArray(

return array_merge_recursive(  // should be able to merge partial arrays together

  $preConfig,

  array(

    // append string from params array

    'name'=>'My Web Application '. $preConfig['params']['nameSuffix'],

    ...

    'import'=>array(

      ...

    ),

    'modules'=>array(

      ...

    ),

    'components'=>array(

      ...

    ),

    ...

  ),

);



/Tommy

(My apologies if this has been suggested before)

If only a basePath is needed… you can just declare a variable $basePath instead of an array and array_merge_recursive()…

@tri: Be careful with array_merge_recursive, i just learned from this thread that it doesn’t always produce what one would expect:

http://www.yiiframew…-vs-cjsonencode