dongmacuo1193 2014-12-10 14:00 采纳率: 0%
浏览 166
已采纳

自定义URL规则,包含Yii2中的模块

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 controllers. My rules for the small modules work fine but the ones of the big modules won't. Here are my rules:

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

The first two rules work fine, allowing me to access: http://host/news/create and routes to news/default/create.

The last two are supposed to do the following: http://host/posts/category which should route to posts/category/index and http://host/posts/category/create which should route to posts/category/create

They do not seem to work, sadly. Any suggestions?

  • 写回答

1条回答 默认 最新

  • drtldt55533 2014-12-10 14:09
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?