Let's take a look at the application initialization happening in the index.php
entry point file:
require(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php'), $config = require(__DIR__ . '/../config/web.php'), (new yiiwebApplication($config))->run();
As per the ideology of Yii, the preceding three lines mean the following:
Yii
class definition.yiiwebApplication
instance.components
section of $config
and attach them to the application instance.$config
like setting up the application's own attributes.yiiwebApplication
instance to the Yii
class as the Yii::$app
static variable.In step 3, the components are created using the following rule: each array inside the components
section of the application config is transformed into the instance of the class mentioned in the class
key of the array, and other key/value pairs define the initial values that should be set for the properties of these instances. This rule is recursive. There are some special components for which the application knows the default classes beforehand. These components will be attached to the application as properties whose names are equal to the key/value pairs of the configuration in the components
section.
For example, consider the following configuration snippet from the components
section, perfectly fitting our didactic purposes:
'log' => [ 'traceLevel' => 3, 'targets' => [ [ 'class' => 'yiilogFileTarget', 'levels' => ['error', 'warning'], ], ], ],
This configuration means that the following code will be executed during the creation of the application:
$log = new yiilogLogger; // default class name $log->traceLevel = 3; $fileTarget = new yiilogFileTarget; $fileTarget->levels = ['error', 'warning']; $log->targets = [$fileTarget]; Yii::$app->log = $log;
There is a whole bunch of components that are attached by default to any application. Their IDs, by which they are mentioned in the configuration, are already known, so Yii 2 knows which classes to use for these components even if you don't specify them.
Also, basically, each array that has a key named class
and a valid class name as the value of this key will be treated as the configuration for the instance of that class.
These rules are encapsulated in the Yii::createObject()
invocation. You can always consult the source code for an enthralling adventure into the Dependency Injection Containers implementation, which we will skip in this book both because these implementation details are irrelevant to us and to save our sanity overall.
As soon as the log
component is generated, it is attached to the application as the log
property. As a result, you'll be able to reach this newly created instance of the yiilogLogger
component with the following incantation:
Yii::$app->log
The logger is not so useful in this case though as it is usually used only indirectly through the Yii::error()
, Yii::warning()
, Yii::info()
, and Yii::trace()
calls (you probably already know what they do).
Here is a list of the components attached to both yiiconsoleApplication
and yiiwebApplication
by default, whether you specify the configuration for them or not:
Component ID |
Component class |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
The following components are attached only to the console application:
Component ID |
Component class |
---|---|
|
|
|
|
|
|
The following components are attached only to the web application:
Component ID |
Component class |
---|---|
|
|
|
|
|
|
|
|
|
|
Note that both the console and web applications have the response
, request
, and errorHandler
components, but they are of different classes.
In case Yii 2 updates itself in some way or another, you can always see the actual list of the built-in components inside the definition of the Application.coreComponents()
method.
The most important point of this system of components is that nothing prevents you from creating and registering your own component. It is indistinguishable from the built-in components.
3.129.42.243