S
S
Sergey Belyaev2017-01-06 01:49:07
Yii
Sergey Belyaev, 2017-01-06 01:49:07

Where is the correct place to register dependencies (DI) in Yii2?

Since Yii 2.0.11 dependencies (Dependency Injection) can be registered in the application configuration using container, but at the moment the latest version is 2.0.10 and therefore the question arises where exactly are dependencies registered in current versions?
Yii::$container->set(...);
The second question is: where is the right place?
A friend told me about bootstrap, but I do not want to add something to bootstrap if it is guaranteed not to be used. On the documentation and on the Yii2 forum, I did not find a clear answer.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander, 2017-01-06
@p0vidl0

The yii\di\Container::set() method is short enough to fit into bootstrap.
Alternatively, you can register DI when initializing modules or components in which it will be used. Moreover, it is not necessary to use Yii::$container, you can have your own DI Container in the corresponding module.

A
Alexander Makarov, 2017-01-06
@SamDark

Any config file. Container configuration is not dependency initiation.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question