Codeigniter-获取当前路线 [英] Codeigniter - Get current route
本文介绍了Codeigniter-获取当前路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在寻求帮助,以了解我的Codeigniter应用程序经过的路线.
I'm looking for help to know which route my Codeigniter application goes through.
在config/routes.php的应用程序文件夹中,我得到了一些数据库生成的路由,看起来可能像这样:
In my application folder in config/routes.php i got some database generated routes, could look like this:
$route["user/:any"] = "user/profile/$1";
$route["administration/:any"] = "admin/module/$1";
例如,如果我要转到domain.net/user/MYUSERNAME,那么我想知道我通过了"user/:any"路由.
有可能知道它遵循的路线吗?
If i for example to go domain.net/user/MYUSERNAME, then i want to know that i get through the route "user/:any".
Is it possible to know which route it follows?
推荐答案
我用@Ochi的答案来解决这个问题.
I used @Ochi's answer to come up with this.
$routes = array_reverse($this->router->routes); // All routes as specified in config/routes.php, reserved because Codeigniter matched route from last element in array to first.
foreach ($routes as $key => $val) {
$route = $key; // Current route being checked.
// Convert wildcards to RegEx
$key = str_replace(array(':any', ':num'), array('[^/]+', '[0-9]+'), $key);
// Does the RegEx match?
if (preg_match('#^'.$key.'$#', $this->uri->uri_string(), $matches)) break;
}
if ( ! $route) $route = $routes['default_route']; // If the route is blank, it can only be mathcing the default route.
echo $route; // We found our route
这篇关于Codeigniter-获取当前路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文