Applications are service locators. They host a set of the so-called application components that provide different services for processing requests. For example, the urlManager
component is responsible for routing Web requests to appropriate controllers; the db
component provides DB-related services; and so on.
Each application component has an ID that uniquely identifies itself among other application components in the same application. You can access an application component through the expression:
\Yii::$app->componentID
For example, you can use \Yii::$app->db
to get the DB connection, and \Yii::$app->cache
to get the primary cache registered with the application.
An application component is created the first time it is accessed through the above expression. Any further accesses will return the same component instance.
Application components can be any objects. You can register them by configuring the yii\base\Application::$components property in application configurations. For example,
[ 'components' => [ // register "cache" component using a class name 'cache' => 'yii\caching\ApcCache', // register "db" component using a configuration array 'db' => [ 'class' => 'yii\db\Connection', 'dsn' => 'mysql:host=localhost;dbname=demo', 'username' => 'root', 'password' => '', ], // register "search" component using an anonymous function 'search' => function () { return new app\components\SolrService; }, ], ]
Info: While you can register as many application components as you want, you should do this judiciously. Application components are like global variables. Using too many application components can potentially make your code harder to test and maintain. In many cases, you can simply create a local component and use it when needed.
As mentioned above, an application component will only be instantiated when it is being accessed the first time. If it is not accessed at all during a request, it will not be instantiated. Sometimes, however, you may want to instantiate an application component for every request, even if it is not explicitly accessed. To do so, you may list its ID in the bootstrap property of the application.
For example, the following application configuration makes sure the log
component is always loaded:
[ 'bootstrap' => [ 'log', ], 'components' => [ 'log' => [ // configuration for "log" component ], ], ]
Yii defines a set of core application components with fixed IDs and default configurations. For example, the request component is used to collect information about a user request and resolve it into a route; the db component represents a database connection through which you can perform database queries. It is with help of these core application components that Yii applications are able to handle user requests.
Below is the list of the predefined core application components. You may configure and customize them like you do with normal application components. When you are configuring a core application component, if you do not specify its class, the default one will be used.
© 2008–2017 by Yii Software LLC
Licensed under the three clause BSD license.
http://www.yiiframework.com/doc-2.0/guide-structure-application-components.html