@ -61,8 +61,10 @@ 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. This allows you to configure and register multiple components at once. The following code shows a configuration array
a writable property named [[yii\di\ServiceLocator::setComponents()|components]] is provided. This allows you
that can be used to configure an application, while also registering the "db", "cache" and "search" components:
to configure and register multiple components at once. The following code shows a configuration array
that can be used to configure a service locator (e.g. an [application](structure-applications.md) with
the "db", "cache" and "search" components:
```php
```php
return [
return [
@ -76,36 +78,40 @@ return [
],
],
'cache' => 'yii\caching\ApcCache',
'cache' => 'yii\caching\ApcCache',
'search' => function () {
'search' => function () {
return new app\components\SolrService;
$solr = new app\components\SolrService('127.0.0.1');
// ... other initializations ...
return $solr;
},
},
],
],
];
];
```
```
In complex cases it's a good idea to create extra class to hide configuration complexity:
In the above, there is an alternative way to configure the "search" component. Instead of directly writing a PHP
callback which builds a `SolrService` instance, you can use a static class method to return such a callback, like