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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀