Adding Routing Rules Dynamically For Modules

I have a restful api app and I am trying to load different urls for different api versions (each version a module).

I understand routing rules can be added dynamically by implementing the BootstrapInterface on the Module, and adding rules in the implementation of the bootstrap() method a la http://www.yiiframework.com/doc-2.0/guide-runtime-routing.html#adding-rules

This requires registering the component in bootstrap in the main config file, e.g.


'bootstrap' => ['v1', 'v2'],

The above is both costly (requires registering both Modules when only one is ever required) and, more importantly for my query here, defeats the purpose of having separate rules as they are both loaded. The best that can be done, as I see it, is to just include the urls in the main config file, e.g.


'GET,HEAD <module>/account/<code>' => '<module>/account/view', // both v1 and v2

'POST v1/account/signup' => 'v1/account/signup',

'POST v2/account/create' => 'v2/account/create',

Or am I missing something?

I think you are right.

You have to have the url rules for both v1 and v2 before you choose the module to load. Choosing the module requires the parsing of the requested url. ;)