Using the configuration file, explain what can be configured.

The purpose of this post is to let anyone quickly find how to accomplish a configuration task, and list here all the posibilities of the config.php file and also link to pages that explain how is each thing implemented.

Everyone is welcome to add here configuration rules that haven't been specified or links to other documentation.

The configuration file:

<?php
 
// uncomment the following to define a path alias
// Yii::setPathOfAlias('local','path/to/local-folder');
 
// This is the main Web application configuration. Any writable
// CWebApplication properties can be configured here.
return array(
 
    // the base path to the protected folder
    // access it using Yii::app()->basePath
    'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',
 
    // name of application
    // access it using Yii::app()->name
    'name'=>'My website',
 
    //aliases to specify shortcuts for paths in your app or
      // perhaps to external resources for your app
      'aliases'=>array(
            'myExternalFramework'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..'
            .DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'myexternalframework'
        ),
 
        //building on above for a controller in the external 
        //framework you can use the controller map to map the 
        //controller path
        'controllerMap'=>array('myController'=>'myExternalFramework.controllers.MyController'),
 
        // default controller that will be executed when running the application
    'defaultController'=>'site', 
 
    // user language (for Locale)
    'language'=>'es',
 
        //language for messages and views
        'sourceLanguage'=>'es',
 
    // charset to use
    'charset'=>'utf-8',
 
    // preloading application components
    'preload'=>array('log'),
 
    // autoloading classes
    'import'=>array(
        'application.models.*',
        'application.components.*',
    ),
 
    // application-level parameters that can be accessed
    // using Yii::app()->params['paramName']
    'params'=>array(
        'adminEmail'=>'info@example.com',
    ),
 
    // Note: you can also separate your configuration file into
    // more files, for example: db.php, params.php and so on.
    // You can do it like this example:
    // 'params'=>require(dirname(__FILE__).'/params.php'),
    // and inside the params.php you will return the array:
    // return array('adminEmail'=>'info@example.com');
 
    // configuration of application components
    'components'=>array(
 
        // assets, see http://www.yiiframework.com/doc/api/CAssetManager
        'assetManager'=>array(
            // change the path on disk
            'basePath'=>dirname(__FILE__).'/../../assets/',
            // change the url
            'baseUrl'=>'/web/assets/'
        ),
 
        // logging
        'log'=>array(
            // class of logger
            'class'=>'CLogRouter', 
            // where to store logs?
            'routes'=>array( 
                array(
                    // store on file, other options are available
                    'class'=>'CFileLogRoute', 
                    // what to store on file? error and warning, info and trace can be added here
                    'levels'=>'error, warning', 
                ),
            ),
        ),
 
        // user
        'user'=>array(
            // enable cookie-based authentication
            'allowAutoLogin'=>true,
            // set the url where user must be redirected if authentication needed
            // use null to force 403 HTTP error 
            'loginUrl'=>null,
            // set here the name of a class
            // that extends CWebUser and it is stored in
            // protected/components/<classname>
            // see: http://www.yiiframework.com/doc/cookbook/60/
            'class' => 'WebUser',
        ),
 
        // database
        'db'=>array(
            // using mysql
            'connectionString'=>'mysql:host=example.com;dbname=my_db',
            'username'=>'my_user',
            'password'=>'my_password',
            // set the charset of the connection
            'charset'=>'utf8',
            // using sqlite
            // 'connectionString'=>'sqlite:'.dirname(__FILE__).'/../data/blog.db',
            //'charset'=>'utf8',
            'schemaCachingDuration'=>'duration in seconds',
        ),
 
        // caching
        'cache'=>array(
            'class'=>'A cache class, like: system.caching.CApcCache',
        ),
 
        // url 
        'urlManager'=>array(
            // the URL format. It must be either 'path' or 'get'.
            // path: index.php/controller/action/attribute/value
            // get: index.php?r=controller/action&attribute=value
            'urlFormat'=>'path',
            // show www.example.com/index.php/controller/action 
            // or just www.example.com/controller/action
            'showScriptName' => true,
            // rules to redirect a specific url to the controller you want
            // see: http://www.yiiframework.com/doc/guide/topics.url
            'rules'=>array(
                // www.example.com/home instead of www.example.com/site/index
                'home'=>'site/index',
                'post/<id:\d+>'=>'post/show',
            ),
        ),
                // you can use the scriptMap to configure where your scripts come from. 
                //If you use the split configurations for development and production you can 
                // have different maps in each and then just load the file and it'll
                // load the appropriate file depending on the configuration your running.
                // for a production configuration you can have this
                'clientScript'=>array(
                      'scriptMap'=>array(
                          'register.js'=>'site.min.js',
                          'login.js'=>'site.min.js',
                      ),
                ),
                // for a development configuration you can have this
                'clientScript'=>array(
                      'scriptMap'=>array(
                          'register.js'=>'register.js',
                          'login.js'=>'login.js',
                      ),
                ),
    ),
);
?>

Total 7 comments:

#973
Absolute great
by mintao at 1:47pm on January 11, 2010.

Thank you very very much. I'm one of these people learning much more on shown examples than api documentations and docblocks.

#974
Thanks
by colt at 1:18pm on January 11, 2010.

Very good for newbies and intermediate I believe :-)

#985
the scriptMap is interesting
by atrandafir at 1:33am on January 13, 2010.

thanks for showing the production/development example

#1008
Nice
by webscriptz at 1:02am on January 17, 2010.

I've done something similar on my blog a few weeks ago, only i divided the config into separate files. I know it's a bit of a speed decline but it's cleaner now. Certainly for big applications in the development stage

#1019
util
by megabr at 1:59am on January 19, 2010.

bastante util

#1058
thats fantastic
by scoob.junior at 1:37pm on January 30, 2010.

the whole Yii's documentation should be this way!!!!

thanks very much

#1226
dirname()
by ~CODER~ at 3:44pm on March 3, 2010.

Why not use 'basePath'=>dirname(dirname(__FILE__))
instead of 'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..' ?

Your Comment:

You may enter comment using Markdown syntax.

Please login with your forum account.
Note: you must have at least ONE forum post with your account.