UrlManager: unordered optional parameters

Hi everyone!

It seems I have faced UrlManager feature that worked in Yii1.1 but is not easily supported in Yii2.

For example, I have "smart filter" of tours on my site which may lead to generation of the following URLs:

/location-london/sight-big-ben

/location-london/sight-big-ben/type-walking

/location-london/persons-2

/location-london//type-walking/persons-2

From what I have found in Yii1.1 I could write the following simple UrlManager RegEx rule to handle all such requests at once:




'location-<slug>/(/sight-<sightslug>)?(/type-<movementtype>)?(/persons-<personsnum>)?' => 'tour/tour/by-location'



However in Yii2 this doesn’t seem to be possible anymore.

Instead Yii2 docs suggest using ‘default’ property (http://www.yiiframework.com/doc-2.0/guide-runtime-routing.html#parameterizing-routes):




[

                    'pattern' => 'location-<slug>/<sightslug>/<personnum>/...',

                    'route' => 'tour/tour/by-location',

                    'defaults' => ['sightslugs' => null, 'personnum' => 1, ...]

                ],



but this is not the same at all because you cannot rotate the sequence of parameters in this case.

Eventually I figured our the inelegant way by using the rule like this:




                [

                    'pattern' => 'location-<slug>/<sightslugs:sight-[\w-]+>/<personsnum:persons-[\d]+>',

                    'route' => 'tour/tour/by-location',

                    'defaults' => ['sightslugs' => null, 'personsnum' => null]

                ],



but this requires additional treatment in controller to remove all those ‘sight-’ and ‘persons-’ prefixes.

Are there any neat ways to simply re-use the old RegEx logic in the pattern? Or it only can be done with custom Rule class?

Thanks a lot for any help in advance!

I know it’s an old questions, but I am facing the same issue right now. Did you find a way?