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.

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

报告相同问题?

悬赏问题

  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了