CodeIgniter - When using $route['(:any)']

2019-01-20 14:49发布

When using

$route['(:any)'] = 'pages/view/$1';

and I want to use other controllers in my routing for example:

$route['del/(:any)'] = 'crud/del';

it won't work. I guess it will use

pages/view/del/$1

and not my crud-controller when deleting an item. How can I solve this?

3条回答
仙女界的扛把子
2楼-- · 2019-01-20 15:16

Its hundred percent working

$route['(:any)'] url is placed last in your routes file

$route['(:any)/company_product_deal_detail']    =   "mypage_product_picture/deal_detail/$1";
$route['(:any)/company_service_deals/(:any)']    =   "mypage_service_deal_list/index/$1";
$route['(:any)/company_service_deals']    =   "mypage_service_deal_list/index/$1";

$route['(:any)']    =   "company/index/$1";
查看更多
劳资没心,怎么记你
3楼-- · 2019-01-20 15:20

As indicated, $route['(:any)'] will match any URL, so place your other custom routes before the "catch-all" route:

$route['del/(:any)'] = 'crud/del';
// Other routes as needed...
$route['(:any)'] = 'pages/view/$1';
查看更多
Root(大扎)
4楼-- · 2019-01-20 15:32

I know that it's an old question, but I have found myself a nice solution.

By default, CodeIgniter gives priority to URL's from routes config (even if straight controller, method etc. specified), so I have reversed this priority this way:

In system/core/Router.php find _parse_routes method.

Add this code under literal route match:

$cont_segments = $this->_validate_request($this->uri->segments);
if ($cont_segments == $this->uri->segments) {
  return $this->_set_request($cont_segments);
}

I agree, that this approach is kinda wrong, because we edit file from system/core, but I needed a fast soluttion to work with a lot of URL's.

查看更多
登录 后发表回答