Configuring components
Yii is a very customizable framework. Moreover, as in all customizable code, there should be a convenient way to set up different application parts. In Yii, this is provided through configuration files located at config
.
Getting ready
Create a new application by using the Composer package manager as described in the official guide at http://www.yiiframework.com/doc-2.0/guide-startinstallation.html.
How to do it…
If you have worked with Yii before, then you have probably configured a database connection:
return [ … 'components' => [ 'db' => [ 'class' => 'system.db.CDbConnection', 'dsn' => 'mysql:host=localhost;dbname=database_name', 'username' => 'root', 'password' => '', 'charset' => 'utf8', ], … ], … ];
This way of configuring components 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 an 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 application configuration file. In the preceding code, dsn
value will be assigned to yii\db\Connection::dsn
, username
will be assigned to Connection::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 the case of the db
component, the class is yii\db\Connection
. You can just open the class and look for its public properties, which you can set from config.
In the preceding code, the class
property is a bit special because it is used to specify the component class name. It does not exist in the yii\db\Connection
class. Therefore, it can be used to override a class as follows:
return [ … 'components' => [ 'db' => [ 'class' => app\components\MyConnection', … ], … ], … );
This way, you can override each application component; this is very useful whenever a standard component does not fit your application.
Built-in components
Now, let's find out which standard Yii application components you can configure. There are two application types bundled with Yii:
- Web application (
yii\web\Application
) - Console application (
yii\console\Application
)
Both are extended from yii\base\Application
, so both console and web applications share its components.
You can get the component names from the source code of the coreComponents()
application's method.
You can add your own application components (classes extended from yii\base\Component
) by simply adding new configuration items and pointing their class properties to your custom classes.
See also
- Both console and web application components are listed in the list at http://www.yiiframework.com/doc-2.0/guide-structure-application-components.html
- For more information on creating your own components see:
- The Service locator recipe
- The Creating components recipe in Chapter 8, Extending Yii