The Yii application components

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:

  1. Set up the Yii class definition.
  2. Create the yiiwebApplication instance.
  3. Create various components declared in the components section of $config and attach them to the application instance.
  4. Next, perform all the other processing of $config like setting up the application's own attributes.
  5. Attach the loaded 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.

Tip

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

log

yiilogDispatcher

formatter

yiiaseFormatter

i18n

yiii18nI18N

mailer

yiiswiftmailerMailer

urlManager

yiiwebUrlManager

view

yiiwebView

assetManager

yiiwebAssetManager

security

yiiaseSecurity

The following components are attached only to the console application:

Component ID

Component class

request

yiiconsoleRequest

response

yiiconsoleResponse

errorHandler

YiiconsoleErrorHandler

The following components are attached only to the web application:

Component ID

Component class

request

yiiwebRequest

response

yiiwebResponse

session

yiiwebSession

user

yiiwebUser

errorHandler

yiiwebErrorHandler

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.42.243