mirror of
https://github.com/yiisoft/yii2.git
synced 2025-08-15 23:04:54 +08:00
Edited; very clean!
This commit is contained in:
@ -2,19 +2,19 @@ Service Locator
|
|||||||
===============
|
===============
|
||||||
|
|
||||||
A service locator is an object that knows how to provide all sorts of services (or components) that an application
|
A service locator is an object that knows how to provide all sorts of services (or components) that an application
|
||||||
might need. Within a service locator, each component has only a single instance which is uniquely identified by an ID.
|
might need. Within a service locator, each component exists as only a single instance, uniquely identified by an ID.
|
||||||
You use the ID to retrieve a component from the service locator.
|
You use the ID to retrieve a component from the service locator.
|
||||||
|
|
||||||
In Yii, a service locator is simply an instance of [[yii\di\ServiceLocator]] or its child class.
|
In Yii, a service locator is simply an instance of [[yii\di\ServiceLocator]], or from a child class.
|
||||||
|
|
||||||
The most commonly used service locator in Yii is the *application* object which can be accessed through
|
The most commonly used service locator in Yii is the *application* object, which can be accessed through
|
||||||
`\Yii::$app`. The services it provides are called *application components*, such as the `request`, `response`,
|
`\Yii::$app`. The services it provides are called *application components*, such as the `request`, `response`, and
|
||||||
`urlManager` components. You may configure these components or even replace them with your own implementations easily
|
`urlManager` components. You may configure these components, or even replace them with your own implementations, easily
|
||||||
through functionality provided by the service locator.
|
through functionality provided by the service locator.
|
||||||
|
|
||||||
Besides the application object, each module object is also a service locator.
|
Besides the application object, each module object is also a service locator.
|
||||||
|
|
||||||
To use a service locator, the first step is to register components. A component can be registered
|
To use a service locator, the first step is to register components with it. A component can be registered
|
||||||
via [[yii\di\ServiceLocator::set()]]. The following code shows different ways of registering components:
|
via [[yii\di\ServiceLocator::set()]]. The following code shows different ways of registering components:
|
||||||
|
|
||||||
```php
|
```php
|
||||||
@ -43,7 +43,7 @@ $locator->set('search', function () {
|
|||||||
$locator->set('pageCache', new FileCache);
|
$locator->set('pageCache', new FileCache);
|
||||||
```
|
```
|
||||||
|
|
||||||
Once a component is registered, you can access it using its ID in one of the following two ways:
|
Once a component has been registered, you can access it using its ID, in one of the two following ways:
|
||||||
|
|
||||||
```php
|
```php
|
||||||
$cache = $locator->get('cache');
|
$cache = $locator->get('cache');
|
||||||
@ -53,7 +53,7 @@ $cache = $locator->cache;
|
|||||||
|
|
||||||
As shown above, [[yii\di\ServiceLocator]] allows you to access a component like a property using the component ID.
|
As shown above, [[yii\di\ServiceLocator]] allows you to access a component like a property using the component ID.
|
||||||
When you access a component for the first time, [[yii\di\ServiceLocator]] will use the component registration
|
When you access a component for the first time, [[yii\di\ServiceLocator]] will use the component registration
|
||||||
information to create a new instance of the component and return it. Later if the component is accessed again,
|
information to create a new instance of the component and return it. Later, if the component is accessed again,
|
||||||
the service locator will return the same instance.
|
the service locator will return the same instance.
|
||||||
|
|
||||||
You may use [[yii\di\ServiceLocator::has()]] to check if a component ID has already been registered.
|
You may use [[yii\di\ServiceLocator::has()]] to check if a component ID has already been registered.
|
||||||
@ -61,9 +61,8 @@ If you call [[yii\di\ServiceLocator::get()]] with an invalid ID, an exception wi
|
|||||||
|
|
||||||
|
|
||||||
Because service locators are often being created with [configurations](concept-configurations.md),
|
Because service locators are often being created with [configurations](concept-configurations.md),
|
||||||
a writable property named [[yii\di\ServiceLocator::setComponents()|components]] is provided so that
|
a writable property named [[yii\di\ServiceLocator::setComponents()|components]] is provided. This allows you to configure and register multiple components at once. The following code shows a configuration array
|
||||||
you can configure it and register multiple components at once. The following code shows a configuration array
|
that can be used to configure an application, while also registering the "db", "cache" and "search" components:
|
||||||
that can be used to configure an application and register the "db", "cache" and "search" components:
|
|
||||||
|
|
||||||
```php
|
```php
|
||||||
return [
|
return [
|
||||||
|
Reference in New Issue
Block a user