For example,
php
a class name
$locator->set('cache', 'yii\caching\FileCache');
a configuration array
$locator->set('db', [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=127.0.0.1;dbname=demo',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
]);
an anonymous function
$locator->set('cache', function ($params) {
return new \yii\caching\FileCache;
});
an instance
$locator->set('cache', new \yii\caching\FileCache);
If a component definition with the same ID already exists, it will be overwritten.
public set ( string $id, mixed $definition ) | ||
$id | string | component ID (e.g. `db`). |
$definition | mixed | the component definition to be registered with this locator. It can be one of the following: - a class name - a configuration array: the array contains name-value pairs that will be used to initialize the property values of the newly created object when [[get()]] is called. The `class` element is required and stands for the the class of the object to be created. - a PHP callable: either an anonymous function or an array representing a class method (e.g. `['Foo', 'bar']`). The callable will be called by [[get()]] to return an object associated with the specified component ID. - an object: When [[get()]] is called, this object will be returned. |