codeigniter 上的路由不起作用

routing at codeigniter doesn't work

我正在学习 codeigniter(我使用的是 3.0.0 版),但是当我尝试使用参数进行路由时遇到了问题

在文件 routes.php 中我有:

$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'

;

在 admin_orders.php 我有:

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

并且在视图中:

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

但是当我按下 Delete 时,应用程序会重新加载页面,如果我尝试不使用 /(:any),该函数会加载并显示消息,其他路由正在运行

当然我做错了什么,我如何使用 codeigniter 3 加载一个带有参数的函数?

您使用 :any 的顺序是错误的,因为这可能会在其余部分之前匹配。即使不是为了安全起见,也应该在更通用的最后列出一些东西,因为当路由器按照规则工作时,它会在它说匹配时停止,如果它是更通用或通用的规则,那么它永远不会到达具体的。正如最佳实践规则一样,应该首先列出更具体的事物,最后列出更通用的事物。这就是为什么在标准条件逻辑中 else 出现在 if 和 ifelse 之后(这是显而易见的),但在这种情况下很容易忽略像这样的简单规则..

另见 https://ellislab.com/codeigniter/user-guide/general/routing.html

重要的部分是

(: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.