Custom URL rules with modules in Yii2

前端 未结 1 1153
感动是毒
感动是毒 2021-01-19 15:14

I have been looking around but haven\'t found what I needed. Basically, I have a few small modules which have just the DefaultController and a few bigger ones with multiple

相关标签:
1条回答
  • 2021-01-19 15:54

    It looks like the first rule will capture any request that could also match the third one.

    Think of it in the terms of its representing regex: w+/w+: as a generic rule for routes in Yii, more prescriptive rules should go on top and less more generic, catch-all rules should be at the bottom.

    Now the best way to obtain what you need would be to do something along the lines of:

    '<module:news>/<action:\w+>' => '<module>/default/<action>',
    '<module:news>/<action:\w+>/<id:\d+>' => '<module>/default/<action>',
    '<module:posts>/<controller:\w+>' => '<module>/<controller>/index',
    '<module:posts>/<controller:\w+>/<action:\w+>' => '<module>/<controller>/<action>'
    

    this way you are explicitly expressing the routes for each of the modules in a clear and immediate way which will also help you in the long-term.

    0 讨论(0)
提交回复
热议问题