Yii2: differences between init() and bootstrap() a

2019-07-16 06:16发布

问题:

I dont understand exactly what is the difference between use init() and bootstrap() on a class.

My case:

I want to add dynamical urls from my module by using Yii::$app->urlManager->addRules(...) but NOT loading the module in order to improve the performance.

So, I thought if bootstraping the module from the main configuration file like: 'bootstrap' => ['mymodule'], the Module::bootstrap() function will be executed ONLY and exclusively. But actually always runs Module::init() function, and then Module::bootstrap().

On this documentation http://www.yiiframework.com/doc-2.0/guide-runtime-routing.html#adding-rules say:

'Note that you should also list these modules in yii\web\Application::bootstrap() so that they can participate the bootstrapping process.'

But Module::bootstrap() is not executed if the module is listed on yii\web\Application::bootstrap()

I want to set only the dynamic rules with no module loading. How is it possible? What is the best place to set dynamical URLs with no impact to performance?

回答1:

i decide resolve this issue(adding dynamic rules for module) by watching working extensions.

for example https://github.com/dmstr/yii2-pages-module extension uses bootstrap interface.

don`t forget write in composer.json "type" attribute as "yii2-extension".



标签: yii2