- Yii 1.1 Application Development Cookbook
- Alexander Makarov
- 451字
- 2021-04-02 18:40:58
Configuring components
Yii is a very customizable framework. Moreover, as in every customizable code, there should be a convenient way to setup different application parts. So in Yii, this is provided through a configuration file named main.php
located at protected/config/
.
How to do it…
If you have worked with Yii before, then you have probably configured a database connection:
return array( … 'components'=>array( 'db'=>array( 'class'=>'system.db.CDbConnection', 'connectionString'=>'mysql:host=localhost;dbname=database_name', 'username'=>'root', 'password'=>'', 'charset'=>'utf8', ), … ), … );
This way of configuring component is used when you want to use a component across all application parts. With the preceding configuration, you can access a component by its name, such as Yii::app()->db
.
How it works…
When you are using the Yii::app()->db
component for the first time directly or through active record model, Yii creates a component and initializes its public properties with the corresponding values provided in db
array under the components
section of the main.php
application configuration file. In the preceding code, 'connectionString'
the value will be assigned to CDbConnection::connectionString
, 'username'
will be assigned to CDbConnection::username
, and so on.
If you want to find out what 'charset'
stands for or want to know what else you can configure in the db
component, then you need to know its class. In case of db component, the class is CDbConnection
. You can refer to its API page at http://www.yiiframework.com/doc/api/CDbConnection/ and look for its public properties you can set from config.
In the preceding code, the 'class'
property is a bit special because it is used to specify component class name. It does not exist in the CDbConnection
class. Therefore, it can be used to override a class as follows:
return array( … 'components'=>array( 'db'=>array( 'class'=>'application.components.MyDbConnection', … ), … ), … );
This way, you can override each application component and it is very useful whenever a standard component does not fit your application.
There's more...
Now, let's find out which standard Yii application components you can configure. There are two application types bundled with Yii which are as follows:
- Web application (
CWebApplication
) - Console application (
CConsoleApplication
)
Both are extended from CApplication
, so both console and web applications are sharing its components.
You can get the component names from API pages (http://www.yiiframework.com/doc/api/) and the source code of the registerCoreComponents
application method, but let's list them here so the list can be used as a reference.
Both console and web application components are listed in the following table:

Additional components available only for web application are listed in the following table:

You can add your own application components (classes extended from CComponent
) by simply adding new configuration items and pointing their class
properties to your custom classes.
See also
- The recipe named Configuring widget defaults in this chapter
- Instant Vert.x
- IBM Lotus Notes 8.5 User Guide: LITE
- 中文版SolidWorks 2015技術大全
- OpenStack實戰指南
- Creo 6.0快速入門、進階與精通(升級版)
- AI圖像處理:Photoshop+Firefly后期處理技術基礎與實戰
- BIM與Unity 3D開發實例詳解
- Drupal 6 Panels Cookbook
- PostgreSQL Replication
- After Effects 2022從入門到精通
- Premiere Pro CC 2015中文版基礎與實例教程(第4版)
- 剪映專業版:短視頻創作案例教程(全彩慕課版)
- Creo 4.0中文版基礎教程
- 前端函數式演進
- Axure RP8原型設計圖解視頻教程(Web+App)