Thinkphp 修改U方法按路由規則生成url


tp開戶路由后,使用U方法是不會按路由規則生成url的,一般我們是要手動修改模版,把里面的U方法去掉,手動修改鏈接,如果是已經寫好的程序,后期才添加路由,修改起鏈接就太麻煩了

今天無聊就修改了一下U方法,讓它按路由規則生成url,再不用一條條修改模版了哈哈哈哈哈哈

 

下面代碼添加到 /ThinkPHP/Common/functions.php 文件U方法里,大概 263行(if($suffix) 前面):

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);
            }
        }   
    }   
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM