在codeigniter中覆盖路由

时间:2013-01-01 18:19:02

标签: codeigniter url redirect routes

  

可能重复:
  CodeIgniter - When using $route[‘(:any)’] = ‘pages/view/$1’ how to use other controllers?

我正在使用codeigniter中的网址缩短/重定向网络应用,并遇到了我的路线问题。

我的第一条路线是:

$route['(:any)'] = "redirect/index/$1";

这是处理重定向的原因,因此example.com/dwB会转到重定向控制器。

下面我有一些覆盖页面和其他控制器等的路由。当我描述整个网址包括参数等时,它们工作正常但我现在遇到了麻烦,因为我有一些动态网址,如:

 example.com/stats/view/dwB

或facebook认证回复等,我无法明确写出路线。我尝试过使用类似的东西:

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

但这些似乎都没有覆盖第一条路线。这是我的所有routes.php文件。

$route['(:any)'] = "redirect/index/$1";

$route['shorten/create'] = "shorten/create"; // overwrite the previous route

$route['stats/view/(:any)'] = "stats/view/$1"; // allow the stats controller to be used

$route['login'] = "auth/login"; // allow the login(auth) controller to be used
$route['register'] = "auth/register"; // allow the login(auth) controller to be used
$route['auth_social/fblogin'] = "auth_social/fblogin"; // allow the login(auth) controller    to be used
$route['dashboard'] = "dashboard"; // allow the login(auth) controller to be used
$route['auth/logout'] = "auth/logout"; // allow the login(auth) controller to be used
$route['auth'] = "auth/index"; // allow the login(auth) controller to be used

$route['default_controller'] = "pages";

$route['404_override'] = '404';

1 个答案:

答案 0 :(得分:3)

正如我在评论中提到的,将“任意”路线放在自定义路线的末尾,如下所示:

$route['shorten/create'] = "shorten/create"; // overwrite the previous route

$route['stats/view/(:any)'] = "stats/view/$1"; // allow the stats controller to be used

$route['login'] = "auth/login"; // allow the login(auth) controller to be used
$route['register'] = "auth/register"; // allow the login(auth) controller to be used
$route['auth_social/fblogin'] = "auth_social/fblogin"; // allow the login(auth) controller    to be used
$route['dashboard'] = "dashboard"; // allow the login(auth) controller to be used
$route['auth/logout'] = "auth/logout"; // allow the login(auth) controller to be used
$route['auth'] = "auth/index"; // allow the login(auth) controller to be used

// Move "any" route down here...
$route['(:any)'] = "redirect/index/$1";