Home  >  Article  >  PHP Framework  >  What does yii application component mean?

What does yii application component mean?

(*-*)浩
(*-*)浩Original
2019-11-06 09:17:122797browse

The application principal is a service locator, which deploys a set of application components that provide various functions to handle requests. For example, the urlManager component is responsible for processing web page requests and routing them to the corresponding controller, the db component provides database-related services, and so on.

What does yii application component mean?

In the same application, each application component has a unique ID to distinguish other application components. You can access application components through the following expressions . (Recommended learning: yii tutorial)

\Yii::$app->componentID

For example, you can use \Yii::$app->db to obtain the DB connection registered to the application, use \Yii:: $app->cache to get the primary cache registered to the application.

The first time you use the above expression, an application component instance will be created. Subsequent visits will return this instance, and there is no need to create it again.

Application components can be any objects and can be configured in the application main configuration yii\base\Application::$components attribute. For example:

[
    'components' => [
        // 使用类名注册 "cache" 组件
        'cache' => 'yii\caching\ApcCache',
 
        // 使用配置数组注册 "db" 组件
        'db' => [
            'class' => 'yii\db\Connection',
            'dsn' => 'mysql:host=localhost;dbname=demo',
            'username' => 'root',
            'password' => '',
        ],
 
        // 使用函数注册"search" 组件
        'search' => function () {
            return new app\components\SolrService;
        },
    ],
]

Information: Please be careful to register too many application components. Application components are like global variables. Using too many may make testing and maintenance more difficult. Generally, local components can be created when needed.

Boot Startup Component

As mentioned above, an application component will only be instantiated when it is accessed for the first time. If there is no access during the processing of the request, it will not be instantiated. Sometimes you want to instantiate a component during every request processing even if it is never accessed. You can add the component ID to the bootstrap attribute of the application body.

You can also use closures to boot custom components. There is no need to directly return an instantiated component. The closure is also called after the application body yii\base\Application is instantiated.

For example, the following application body configuration ensures that the log component is always loaded.

[
    'bootstrap' => [
        'log',
        function($app){
            return new ComponentX();
        },
        function($app){
            // 可以写自定义的代码
           return;
        }
    ],
    'components' => [
        'log' => [
            // "log" 组件的配置
        ],
    ],
]

Core application components

Yii defines a set of core components with fixed IDs and default configurations. For example, the request component is used to collect user requests and parse routes; db represents A database connection that can perform database operations. Through these components, the Yii application body can handle user requests.

The following is a list of predefined core application components, which can be configured and customized like ordinary application components. When you configure a core component and do not specify its class name, the class specified by Yii by default will be used.

The above is the detailed content of What does yii application component mean?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn