Home > php教程 > php手册 > body text

3.1路由支持自定义

WBOY
Release: 2016-06-07 11:42:08
Original
1221 people have browsed it

大家都知道SEO的重要性,官方提供的路由不支持随心所遇的自定义URL;在cms项目开发时这个问题是很大的困扰。未解决这个经过反复测试终于修成正果了。
基于官方提供的路由插件小做修改让路由实现自定义。
并不是很完善,可以完全兼容根目录访问。不支持后缀和二级目录!
哪位大神如果感兴趣可以继续修改改善。我是在3.1版本上做的修改,不过大致看了下在3.2版本稍作修改也是通用的。废话不多少直接贴代码了
首先修改下U方法
最终效果显示 例如访问user模块的reg方法 显示结果为 http://ml.topcms.pw/reg 这里的reg你可以自定义随意修改无需配置服务器function U($url='',$vars='',$suffix=true,$redirect=false,$domain=true) {<br>     // 解析URL<br>     $info   =  parse_url($url);<br>     $url    =  !empty($info['path'])?$info['path']:ACTION_NAME;<br>     if(false !== strpos($url,'@')) { // 解析域名<br>         list($url,$host)    =   explode('@',$info['path'], 2);<br>     }<br>     // 解析子域名<br>     if(isset($host)) {<br>         $domain = $host.(strpos($host,'.')?'':strstr($_SERVER['HTTP_HOST'],'.'));<br>     }elseif($domain===true){<br>         $domain = $_SERVER['HTTP_HOST'];<br>         if(C('APP_SUB_DOMAIN_DEPLOY') ) { // 开启子域名部署<br>             $domain = $domain=='localhost'?'localhost':'www'.strstr($_SERVER['HTTP_HOST'],'.');<br>             // '子域名'=>array('项目[/分组]');<br>             foreach (C('APP_SUB_DOMAIN_RULES') as $key => $rule) {<br>                 if(false === strpos($key,'*') && 0=== strpos($url,$rule[0])) {<br>                     $domain = $key.strstr($domain,'.'); // 生成对应子域名<br>                     $url    =  substr_replace($url,'',0,strlen($rule[0]));<br>                     break;<br>                 }<br>             }<br>         }<br>     }<br> <br>     // 解析参数<br>     if(is_string($vars)) { // aaa=1&bbb=2 转换成数组<br>         parse_str($vars,$vars);<br>     }elseif(!is_array($vars)){<br>         $vars = array();<br>     }<br>     if(isset($info['query'])) { // 解析地址里面参数 合并到vars<br>         parse_str($info['query'],$params);<br>         $vars = array_merge($params,$vars);<br>     }<br> <br>     // URL组装<br>     $depr = C('URL_PATHINFO_DEPR');<br>     if($url) {<br>         if(0=== strpos($url,'/')) {// 定义路由<br>             $route      =   true;<br>             $url        =   substr($url,1);<br>             if('/' != $depr) {<br>                 $url    =   str_replace('/',$depr,$url);<br>             }<br>         }else{<br>             if('/' != $depr) { // 安全替换<br>                 $url    =   str_replace('/',$depr,$url);<br>             }<br>             // 解析分组、模块和操作<br>             $url        =   trim($url,$depr);<br>             $path       =   explode($depr,$url);<br>             $var        =   array();<br>             $var[C('VAR_ACTION')]       =   !empty($path)?array_pop($path):ACTION_NAME;<br>             $var[C('VAR_MODULE')]       =   !empty($path)?array_pop($path):MODULE_NAME;<br>             if(C('URL_CASE_INSENSITIVE')) {<br>                 $var[C('VAR_MODULE')]   =   parse_name($var[C('VAR_MODULE')]);<br>             }<br>             if(!C('APP_SUB_DOMAIN_DEPLOY') && C('APP_GROUP_LIST')) {<br>                 if(!empty($path)) {<br>                     $group                  =   array_pop($path);<br>                     $var[C('VAR_GROUP')]    =   $group;<br>                 }else{<br>                     if(GROUP_NAME != C('DEFAULT_GROUP')) {<br>                         $var[C('VAR_GROUP')]=   GROUP_NAME;<br>                     }<br>                 }<br>                 if(C('URL_CASE_INSENSITIVE') && isset($var[C('VAR_GROUP')])) {<br>                     $var[C('VAR_GROUP')]    =  strtolower($var[C('VAR_GROUP')]);<br>                 }<br>             }<br>         }<br>     }<br> <br>     if(C('URL_MODEL') == 0) { // 普通模式URL转换<br>         $url        =   __APP__.'?'.http_build_query(array_reverse($var));<br>         if(!empty($vars)) {<br>             $vars   =   urldecode(http_build_query($vars));<br>             $url   .=   '&'.$vars;<br>         }<br>     }else{ // PATHINFO模式或者兼容URL模式<br>         if(C('URL_ROUTER_ON') && (C('URL_MODEL') == 2) && ($var[C('VAR_GROUP')] == '' || $var[C('VAR_GROUP')] == 'Home') ){<br>             $url        =   __APP__.'?'.http_build_query(array_reverse($var));<br>             if(!empty($vars)) {<br>                 $vars   =   urldecode(http_build_query($vars));<br>                 $url   .=   '&'.$vars;<br>             }<br>             if( is_array(C('URL_ROUTE_RULES')) ){<br>                 foreach (C('URL_ROUTE_RULES') as $route_key => $route_val) {<br>                     preg_match('#'.$route_val.'$#', $url ,$metches);<br>                     if($metches){<br>                        $route_url=$metches;<br>                        $rule=$route_key;<br>                     }<br>                 }<br>                 if($route_url){<br>                     $url_new=explode(':', $rule);<br>                     $u=$url_new[0];<br>                     for($i=1;$i<count></count>                         $u.=$route_url[$i].'/';<br>                     }<br>                     $u=  trim($u,'/');<br>                     $url=str_replace ('/', $depr, $u);<br>                 }<br>             }<br>         }else{<br>             if(isset($route)) {<br>                 $url    =   __APP__.'/'.rtrim($url,$depr);<br>             }else{<br>                 $url    =   __APP__.'/'.implode($depr,array_reverse($var));<br>             }<br>             if(!empty($vars)) { // 添加参数<br>                 foreach ($vars as $v => $val){<br>                     if('' !== trim($val))   $url .= $depr . $v . $depr . urlencode($val);<br>                 }  <br>             }<br>         }<br>         //路由过滤<br> <br>         if($suffix) {<br>             $suffix   =  $suffix===true?C('URL_HTML_SUFFIX'):$suffix;<br>             if($pos = strpos($suffix, '|')){<br>                 $suffix = substr($suffix, 0, $pos);<br>             }<br>             if($suffix && '/' != substr($url,-1)){<br>                 $url  .=  '.'.ltrim($suffix,'.');<br>             }<br>         }<br>     }<br>     if($domain) {<br>         $url   =  (is_ssl()?'https://':'http://').$domain.__ROOT__.'/'.$url;<br>     }<br> <br>     if($redirect) // 直接跳转URL<br>         redirect($url);<br>     else<br>         return $url;<br> }其次对应的修改下路由行为这里我直接把文件附在下面。有兴趣的可以下载看看。
具体效果可以看本人做的项目
http://ml.topcms.pw

附件 CheckRouteBehavior.class.php.zip ( 2.72 KB 下载:40 次 )

AD:真正免费,域名+虚机+企业邮箱=0元

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!