//更改模块配置文件
'URL_ROUTER_ON' => true,
'URL_ROUTE_RULES'=>[]//编写路由优化
//更改模块配置文件
'URL_ROUTER_ON' => true,
'URL_ROUTE_RULES'=>[]//编写路由优化
tp开启路由后,使用U方法是不会按路由规则生成url的,一般我们是要手动修改模版,把里面的U方法去掉,手动修改链接,如果是已经写好的程序,后期才添加路由,修改起链接就太麻烦了
今天无聊就修改了一下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);
}
}
}
}
修改后的U函数如下:
/**
* URL组装 支持不同URL模式
* @param string $url URL表达式,格式:'[模块/控制器/操作#锚点@域名]?参数1=值1&参数2=值2...'
* @param string|array $vars 传入的参数,支持数组和字符串
* @param string|boolean $suffix 伪静态后缀,默认为true表示获取配置值
* @param boolean $domain 是否显示域名
* @return string
*/
function U($url='',$vars='',$suffix=true,$domain=false) {
// 解析URL
$info = parse_url($url);
$url = !empty($info['path'])?$info['path']:ACTION_NAME;
if(isset($info['fragment'])) { // 解析锚点
$anchor = $info['fragment'];
if(false !== strpos($anchor,'?')) { // 解析参数
list($anchor,$info['query']) = explode('?',$anchor,2);
}
if(false !== strpos($anchor,'@')) { // 解析域名
list($anchor,$host) = explode('@',$anchor, 2);
}
}elseif(false !== strpos($url,'@')) { // 解析域名
list($url,$host) = explode('@',$info['path'], 2);
}
// 解析子域名
if(isset($host)) {
$domain = $host.(strpos($host,'.')?'':strstr($_SERVER['HTTP_HOST'],'.'));
}elseif($domain===true){
$domain = $_SERVER['HTTP_HOST'];
if(C('APP_SUB_DOMAIN_DEPLOY') ) { // 开启子域名部署
$domain = $domain=='localhost'?'localhost':'www'.strstr($_SERVER['HTTP_HOST'],'.');
// '子域名'=>array('模块[/控制器]');
foreach (C('APP_SUB_DOMAIN_RULES') as $key => $rule) {
$rule = is_array($rule)?$rule[0]:$rule;
if(false === strpos($key,'*') && 0=== strpos($url,$rule)) {
$domain = $key.strstr($domain,'.'); // 生成对应子域名
$url = substr_replace($url,'',0,strlen($rule));
break;
}
}
}
}
// 解析参数
if(is_string($vars)) { // aaa=1&bbb=2 转换成数组
parse_str($vars,$vars);
}elseif(!is_array($vars)){
$vars = array();
}
if(isset($info['query'])) { // 解析地址里面参数 合并到vars
parse_str($info['query'],$params);
$vars = array_merge($params,$vars);
}
// URL组装
$depr = C('URL_PATHINFO_DEPR');
$urlCase = C('URL_CASE_INSENSITIVE');
if($url) {
if(0=== strpos($url,'/')) {// 定义路由
$route = true;
$url = substr($url,1);
if('/' != $depr) {
$url = str_replace('/',$depr,$url);
}
}else{
if('/' != $depr) { // 安全替换
$url = str_replace('/',$depr,$url);
}
// 解析模块、控制器和操作
$url = trim($url,$depr);
$path = explode($depr,$url);
$var = array();
$varModule = C('VAR_MODULE');
$varController = C('VAR_CONTROLLER');
$varAction = C('VAR_ACTION');
$var[$varAction] = !empty($path)?array_pop($path):ACTION_NAME;
$var[$varController] = !empty($path)?array_pop($path):CONTROLLER_NAME;
if($maps = C('URL_ACTION_MAP')) {
if(isset($maps[strtolower($var[$varController])])) {
$maps = $maps[strtolower($var[$varController])];
if($action = array_search(strtolower($var[$varAction]),$maps)){
$var[$varAction] = $action;
}
}
}
if($maps = C('URL_CONTROLLER_MAP')) {
if($controller = array_search(strtolower($var[$varController]),$maps)){
$var[$varController] = $controller;
}
}
if($urlCase) {
$var[$varController] = parse_name($var[$varController]);
}
$module = '';
if(!empty($path)) {
$var[$varModule] = implode($depr,$path);
}else{
if(C('MULTI_MODULE')) {
if(MODULE_NAME != C('DEFAULT_MODULE') || !C('MODULE_ALLOW_LIST')){
$var[$varModule]= MODULE_NAME;
}
}
}
if($maps = C('URL_MODULE_MAP')) {
if($_module = array_search(strtolower($var[$varModule]),$maps)){
$var[$varModule] = $_module;
}
}
if(isset($var[$varModule])){
$module = $var[$varModule];
unset($var[$varModule]);
}
}
}
if(C('URL_MODEL') == 0) { // 普通模式URL转换
$url = .'?'.C('VAR_MODULE')."={$module}&".http_build_query(array_reverse($var));
if($urlCase){
$url = strtolower($url);
}
if(!empty($vars)) {
$vars = http_build_query($vars);
$url .= '&'.$vars;
}
}else{ // PATHINFO模式或者兼容URL模式
if(isset($route)) {
$url = .'/'.rtrim($url,$depr);
}else{
$module = (defined('BIND_MODULE') && BIND_MODULE==$module )? '' : $module;
$url = .'/'.($module?$module.MODULE_PATHINFO_DEPR:'').implode($depr,array_reverse($var));
}
if($urlCase){
$url = strtolower($url);
}
if(!empty($vars)) { // 添加参数
foreach ($vars as $var => $val){
if('' !== trim($val)) $url .= $depr . $var . $depr . urlencode($val);
}
}
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);
}
}
}
}
if($suffix) {
$suffix = $suffix===true?C('URL_HTML_SUFFIX'):$suffix;
if($pos = strpos($suffix, '|')){
$suffix = substr($suffix, 0, $pos);
}
if($suffix && '/' != substr($url,-1)){
$url .= '.'.ltrim($suffix,'.');
}
}
}
if(isset($anchor)){
$url .= '#'.$anchor;
}
if($domain) {
$url = (is_ssl()?'https://':'http://').$domain.$url;
}
return $url;
}
然后在路由中配置如下(我是在home下面的config.php配置的,然后在index.php绑定了默认模块home,所以下面就不用加home了):
<?php
return array(
//'配置项'=>'配置值'
'URL_MODEL' => 2,
//路由规则
'URL_ROUTER_ON' => true,
'URL_ROUTE_RULES'=>array(
'team/read/:id' => 'team/content',
'anli/read/:id' => 'anli/content',
),
);
此时在前端页面使用U函数,{:U('team/content',array('id'=>$t['art_id']))}(这块注意大小写问题,因为是匹配,我这没有弄不区分大小写)页面输出为xxx.com/team/read/11.html,完工。
本文参考:http://blog.csdn.net/qq_31648761/article/details/56012800
网友评论