Page

[转载]修改核心文件function.php使U方法能根据路由规则生成新的url

683Anson17-08-12


[转载]修改核心文件function.php使U方法能根据路由规则生成新的url


下面代码添加到 /ThinkPHP/Common/functions.PHP 文件,在U方法里面直接搜索if($suffix),在前面加入以下代码,u方法生成的url就是按照路由规则生成的呢!


if(C('URL_ROUTE_RULES')){
    foreach (C('URL_ROUTE_RULES') as $rule=>$real) {
        if(strpos($url, $real)!==false){
            $url = str_replace($real, $rule, $url);
            preg_match("/\/(\w+)\.php\/(\w+)/", $url, $match);
            if(isset($match[1]) && isset($match[2]) && $match[1][0]==$match[2][0]){
                $url = preg_replace("/\/(\w+)\.php/", '', $url);
            }elseif(strpos($url, 'index.php')!==false){
                $url = str_replace("/index.php", '', $url);
            }else{
                $url = str_replace(".php", '', $url);
            }

            preg_match_all("/(:\w+)/", $rule, $matches);
            foreach ((array)$matches[1] as $match) {
                $url = str_replace($match . '/', '', $url);
                $url = str_replace(substr($match, 1) . '/', '', $url);
            }
        }
    }
}



转自

http://blog.csdn.net/qq_31648761/article/details/56012800