Home  >  Article  >  PHP Framework  >  Are Yii framework application components service locators?

Are Yii framework application components service locators?

(*-*)浩
(*-*)浩Original
2019-11-29 15:36:192228browse

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 routing web page requests to the corresponding controller. The db component provides database-related services and more.

Are Yii framework application components service locators?

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 framework)

\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 obtain 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;
        },
    ],
]

Message: 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. Normally you can create local components 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 Are Yii framework application components service locators?. 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