douliedai4838 2012-09-18 16:12
浏览 45
已采纳

codeigniter条件路由

I am trying to setup routing based on the 1st URI segment.

I have a rule:

$route['(:any)'] = pages/view_page/$1;

I want to make my routes conditional so that if a controller of the name of URI segment 1 is called it goes to the controller as normal. If no controller exists then go to the pages/view_page function.

I have tried wrapping the $route rule with an if statement that checks URI segment 1 and uses that to see if a file exists to match it but this doesn't seem to work.

Does anybody have any suggestions for how to do this without having to write lots of route configurations.

  • 写回答

1条回答 默认 最新

  • dqcz57269 2012-09-18 18:03
    关注

    Since the routing overrides are simple regexp based transformations, you can't really embed "is that would work otherwise" kind of logic directly. This leaves you with basically two options:

    1. List the routes you want to be working explicitly before the "catch all" (:any) rule
    2. Use the $route['404_override'] reserved route instead to indicate what controller should be loaded when no controller found otherwise. However, check the urls comming in and call show_404() and output proper 404 http status code for missing static assets and such.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 请求分析基于spring boot+vue的前后端分离的项目
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥200 关于#c++#的问题,请各位专家解答!网站的邀请码
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?