Differences between init() and bootstrap()

Hello. 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?

If I understand it right, the bootstrapping process takes place when the app is created, that is, before the url manager parses the requested URL into the route.

I don’t think it’s possible to add the custom url rules of a module only when the module is called, because the url manager must know how to parse the request into the module’s route in advance before actually loading the module.

For example, the request may be like this: ‘your-module/some-controller/id/some-parameter/some-action’. Then the url manager should be able to parse it into the right route and parameters of your module. Can it be possible without the custom url rules of the module?

[EDIT]

Oh, I’m sorry. I didn’t understand your question very well.

So you want to set the url rules in bootstrapping time, but don’t want to do some heavy initialization process of the module… Let me think again.

It seems to be impossible to add the custom url rules of a module using bootstrap() method without loading (instantiating) the module itself.

Adding Rules Dynamically (http://www.yiiframework.com/doc-2.0/guide-runtime-routing.html#adding-rules)

Bootstrapping (http://www.yiiframework.com/doc-2.0/guide-runtime-bootstrapping.html)

I would consider adding the url rules for the module manually in the main configuration file of the app. ;)