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 關注
- 171 瀏覽
添加回答
舉報