【发布时间】:2013-02-28 07:38:33
【问题描述】:
假设我有一个名为
的控制器页面
还有一个方法
slug_on_the_fly
public function slug_on_the_fly($slug)
我的路线会是什么样子?
例如对于博客控制器,这很容易:
$route['blog/(:any)'] = 'pages/slug_on_the_fly/$1';
然后http://localhost/blog/name-of-the-article 效果很好
但是,如果我想在没有blog 的情况下这样做,例如
http://localhost/name-of-the-article 或 http://localhost/another-article-blablabla
如何做到这一点并且不破坏其他路线,例如$route['friends'] = 'users'; 或 $route['about-us'] = 'pages/about_us'; ?
因为如果我这样做:
$route['(:any)'] = 'pages/slug_on_the_fly/$1';
它可能会毁掉其他一切,还是?
【问题讨论】:
-
知道如何处理这种情况吗?
-
把“catch-all”路由放在最后,如果没有找到任何东西,请确保该方法使用
error_404()。 -
你能说得更具体点吗?如果我把
$route['(:any)'] = 'pages/slug_on_the_fly/$1';放在最后,之前的一切都会搞砸 -
路由是按顺序检查的,所以
(:any)不会匹配,除非它之前没有匹配的其他路由。 -
路由匹配短路。 first 匹配的路由获胜,而不是 last 匹配的路由!即使它没有短路,解决方法是将
(:any)作为 first 路由,对吗?
标签: php codeigniter url-rewriting url-routing