doudou0612 2015-07-21 23:51
浏览 37
已采纳

在codeigniter处路由不起作用

I'm staring learning codeigniter (I'm using version 3.0.0), but i have a porblem when I'm trying routing with parameters

In the file routes.php I have:

$route['admin/orders'] = 'admin_orders/index';
$route['admin/orders/(:any)'] = 'admin_orders/index';
$route['admin/orders/getAll'] = 'admin_orders/getAll';
$route['admin/orders/getLast'] = 'admin_orders/getLast';
$route['admin/orders/delete/(:any)'] = 'admin_orders/delete'

;

In admin_orders.php i have:

 public function delete(){
        $id = $this->uri->segment(4);
         echo "ok   $id";
   }

And in the view:

<a href="'.site_url("admin").'/orders/delete/3'.'" class="btn btn-info">Delete</a>

But when I press the Delete the app reload the page, and if i try without the /(:any) the function loads and show me the message and the other routes are working

Certainly I'mm doing something wrong, how I can load one function with a parameter using codeigniter 3?

  • 写回答

1条回答 默认 最新

  • doupian9798 2015-07-22 00:10
    关注

    Your order of using :any is wrong, as this will likely match before the rest. Even if it's not things should be listed in the more general last just to be safe, because as the router works through the rules it will stop when it says ok that matches, and if it's the more general or generic rules first then it never reaches the specific. Just as a rule of best practice things should be listed the more specific first and more generic last. This is why the else comes after the if and ifelse in standard conditional logic ( which is obvious ) but in cases like this it's easy to overlook simple rules like that..

    see also https://ellislab.com/codeigniter/user-guide/general/routing.html

    the important bits are

    (:num) will match a segment containing only numbers. (:any) will match a segment containing any character.

    Note: Routes will run in the order they are defined. Higher routes will always take precedence over lower ones.

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

报告相同问题?

悬赏问题

  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?