Service Locator
A service locator is an object that understands how to provide services (or components) required by various applications. In the service locator, each component has only one individual instance and is uniquely identified by ID. With this ID, you can get this component from the service locator.
In Yii, a service locator is an instance of [[yii\di\ServiceLocator]] or its subclass.
The most commonly used service locator is the application object, which can be accessed through \ Yii::$app. The services it provides are called application components, such as request, response and urlManager components. You can easily configure these components through the functions provided by the service locator, or even replace them with your own implementation.
In addition to the application object, each module object itself is also a service locator.
To use a service locator, the first step is to register the relevant components. Components can be registered through the [[yii\di\ServiceLocator::set()]] method. The following methods show different ways to register components:
use yii\di\ServiceLocator; use yii\caching\FileCache; $locator = new ServiceLocator; // Register the "cache" component with a class name that can be used to create the component. $locator->set('cache', 'yii\caching\ApcCache'); // Register the "db" (database) component through a configuration array that can be used to create the component. $locator->set('db', [ 'class' => 'yii\db\Connection', 'dsn' => 'mysql:host=localhost;dbname=demo', 'username' => 'root', 'password' => '', ]); // Register the "search" component through an anonymous function that can return the component. $locator->set('search', function () { return new app\components\SolrService; }); // Register the "pageCache" component with the component $locator->set('pageCache', new FileCache);
Once the component is registered successfully, you can access it through its ID in one of the following two ways:
$cache = $locator->get('cache'); // perhaps $cache = $locator->cache;
As shown above, [[yii\di\ServiceLocator]] allows a component to be accessed through the component ID as if it were an attribute value. When you access a component for the first time, [[yii\di\ServiceLocator]] will create an instance of the component through its registration information and return it. Then, if accessed again, the service locator will return the same instance.
You can check whether a component ID is registered through [[yii\di\ServiceLocator::has()]]. If you call [[yii\di\ServiceLocator::get()]] with an invalid ID, an exception will be thrown.
Because of the service locator, it is often attached when it is created configuration information Therefore, we provide a writable attribute named [[yii\di\ServiceLocator::setComponents()|components]], so that you can configure this attribute or register multiple components at one time. The following code shows how to use a configuration array to configure an application and register db, cache, tz and search components:
return [ // ... 'components' => [ 'db' => [ 'class' => 'yii\db\Connection', 'dsn' => 'mysql:host=localhost;dbname=demo', 'username' => 'root', 'password' => '', ], 'cache' => 'yii\caching\ApcCache', 'tz' => function() { return new \DateTimeZone(Yii::$app->formatter->defaultTimeZone); }, 'search' => function () { $solr = new app\components\SolrService('127.0.0.1'); // ... other initializations ... return $solr; }, ], ];
In the above code, there is an alternative way to configure the search component. Instead of directly writing a PHP callback to create a SolrService instance, you can use a static class method to return such a callback, as shown below:
class SolrServiceBuilder { public static function build($ip) { return function () use ($ip) { $solr = new app\components\SolrService($ip); // ... other initializations ... return $solr; }; } } return [ // ... 'components' => [ // ... 'search' => SolrServiceBuilder::build('127.0.0.1'), ], ];
When you release a Yii component to encapsulate some non Yii third-party libraries, this alternative method is the best. When you use the static method shown above to represent a third-party object that builds complex logic, your component users only need to call the static method to configure the component.
Tree traversal
Modules can be nested arbitrarily; Yii application is essentially a module tree. Since each of these modules is a service locator, a child module has access to its parent module. This allows the module to use $this - > get ('db ') instead of referencing the root service locator Yii:: $app - > get ('db'). The added benefit is that developers can override the configuration in the module.
If the module cannot meet the requirements, the request to retrieve the service from the module will be passed to its parent module.
Note that the configuration of components in a module is never merged with the configuration from components in the parent module. The Service Locator pattern allows us to define named services, but we cannot assume that services with the same name use the same configuration parameters.
💖 Those who like this document are welcome to like, collect, leave a message or forward it. Thank you for your support! Author email: zhuzixian520@126.com
This article was first published in LearnKu.com On the website.