ATTENTION: EXPERIMENTAL! Dependency Injection Utilities for YII2

Installs: 3

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 0

Open Issues: 0


dev-master 2019-04-06 09:19 UTC

This package is auto-updated.

Last update: 2020-11-06 13:13:24 UTC


Latest Stable Version License [Build Status (master)]

[Master][Master] [2.7][2.7] [2.6][2.6] [2.5][2.5]
[![Build status][Master image]][Master] [![Build status][2.7 image]][2.7] [![Build status][2.6 image]][2.6] [![Build status][2.5 image]][2.5]
[![Coverage Status][Master coverage image]][Master coverage] [![Coverage Status][2.7 coverage image]][2.7 coverage] [![Coverage Status][2.6 coverage image]][2.6 coverage] [![Coverage Status][2.5 coverage image]][2.5 coverage]

Extended Dependency Injection Container for Yii2


Provides an extended implementation of an Dependency Injection Container for the Yii2 Framework

The following features are added to the standard implementation:

  • Named Parameters: Constructor parameters do not need to be specified by order or index-position, but constructor parameter names can be used optionally.
  • Setter-Calls: It's possible to specify setter-calls in the configuration array using the format methodName(). The value can be a reference (Instance or ReferenceInterface), but must be specified in an array.
  • ReferenceInterface: Additionally to the standard Instance object, a ReferenceInterface is defined which is resolved upon object creation. This component already contains various implementations of ReferenceInterface:
    • InstanceArray: Can be used to define an array of references (Instance-objects or objects implementing ReferenceInterface which are automatically resolved by the Container when needed. Note: The array may contain other values. If a item is not a reference (i.E. a object of another type, a string, etc.) the item is left unchanged.
    • Alias: Can be used to define a path containing an alias, e.g. "@runtime/data/path". This type of reference can be used in the config array if a path is used, but the defined object does not support Yii-Aliases (i.E. 3rd party, not yii-specific classes). using \Amylian\Yii\DI\Alias:of('@app/my/path') does the same as \Yii:getPath('@app/my/path'), but the latter cannot be used in configuration as the aliases are unknown when the configuration is loaded.


The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist amylian/yii2-amylian-di "*"

or add

"amylian/yii2-amylian-di": "*"

to the require section of your composer.json file.


Use the extended Container Class as standard Container in Yii2:

Yii2 creates the global Container by automatically. The easiest way to replace it with an instance of \Amylian\Yii\DI\Container is to add

\Yii::$container = new Amylian\Yii\DI\Container();

right after loading Yii.php:

require __DIR__ . '/../vendor/autoload.php';
require __DIR__ . '/../vendor/yiisoft/yii2/Yii.php';

\Yii::$container = new Amylian\Yii\DI\Container();  // <--- Create New Container!

(new yii\web\Application($config))->run();