3 回答
TA贡献1824条经验 获得超5个赞
Codeigniter 很简单,太简单了...而且因为对我来说这个函数在哪里并不明显(如果存在的话)我刚刚采用 _parse_routes 将 URL(slug)解析到我可以找到的右侧相应的文件就容易多了。
在这里(如果有人遇到与我相同的情况)。
function parseRoute($uri) {
// Get HTTP verb
$http_verb = isset($_SERVER['REQUEST_METHOD']) ? strtolower($_SERVER['REQUEST_METHOD']) : 'cli';
// Loop through the route array looking for wildcards
foreach ($this->router->routes as $key => $val) {
// Check if route format is using HTTP verbs
if (is_array($val)) {
$val = array_change_key_case($val, CASE_LOWER);
if (isset($val[$http_verb])) {
$val = $val[$http_verb];
} else {
continue;
}
}
// Convert wildcards to RegEx
$key = str_replace(array(':any', ':num'), array('[^/]+', '[0-9]+'), $key);
// Does the RegEx match?
if (preg_match('#^' . $key . '$#', $uri, $matches)) {
// Are we using callbacks to process back-references?
if (!is_string($val) && is_callable($val)) {
// Remove the original string from the matches array.
array_shift($matches);
// Execute the callback using the values in matches as its parameters.
$val = call_user_func_array($val, $matches);
}
// Are we using the default routing method for back-references?
elseif (strpos($val, '$') !== FALSE && strpos($key, '(') !== FALSE) {
$val = preg_replace('#^' . $key . '$#', $val, $uri);
}
return $val;
}
}
// If we got this far it means we didn't encounter a
// matching route so we'll set the site default route
return null;
}
现在,这个:
echo parseRoute("novosti/petar")
将产生:
Main/blog/sr/petar
又名:控制器类/该控制器内的函数/语言参数/博客文章
- 3 回答
- 0 关注
- 113 浏览
添加回答
举报