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 questasim仿真报错
  • ¥15 寻找电脑攻防的导师,有问题请教一下。
  • ¥20 微信同是win11,我的电脑安装不了pageoffice,一直无法打开
  • ¥15 这个界面我通过postman请求不到,但是通过浏览器可以正常访问
  • ¥15 动态规划算法实现背包问题
  • ¥15 wpf程序使用过程中异常奔溃
  • ¥15 多目标优化算法在与其他算法数据对比结果判断
  • ¥15 CPTN和EAST,主干网络是VGG16,请问在ICDAR2015数据集上训练之后,CPTN和EAST模型的大小为多少
  • ¥15 按颜色进行点云分割-python
  • ¥15 Matlab如何实现汽车变道切入场景的批量仿真