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 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试