良玉的博客 点点滴滴,积水成河_良玉的博客_页游、手游linux运维工程师之路

Thinkphp通过U方法跨域

改造之后我的U方法 仅供参考

  1. /**

  2.  * URL组装 支持不同URL模式

  3.  * @param string $url URL表达式,格式:'[分组/模块/操作#锚点@域名]?参数1=值1&参数2=值2...'

  4.  * @param string|array $vars 传入的参数,支持数组和字符串

  5.  * @param string $suffix 伪静态后缀,默认为true表示获取配置值

  6.  * @param boolean $redirect 是否跳转,如果设置为true则表示跳转到该URL地址

  7.  * @param boolean $domain 是否显示域名

  8.  * @return string

  9.  */

  10.  function U($url='',$vars='',$suffix=true,$redirect=false,$domain=false) {

  11.     // 解析URL

  12.     $info   =  parse_url($url);

  13.     $url    =  !empty($info['path'])?$info['path']:ACTION_NAME;

  14.     

  15.     

  16.     if(isset($info['fragment'])) { // 解析锚点

  17.         $anchor =   $info['fragment'];

  18.         if(false !== strpos($anchor,'?')) { // 解析参数

  19.             list($anchor,$info['query']) = explode('?',$anchor,2);

  20.         }        

  21.         if(false !== strpos($anchor,'@')) { // 解析域名

  22.             list($anchor,$host)    =   explode('@',$anchor, 2);

  23.         }

  24.     }elseif(false !== strpos($url,'@')) { // 解析域名

  25.         list($url,$host)    =   explode('@',$info['path'], 2);

  26.     }

  27.     

  28.     /*

  29.     * 开启子域名的时候修复url

  30.     * 例如 www.a.com有分组Home,让home.a.com访问的时候U生成的地址home替换www,去掉Home/

  31.     */

  32.     if(C('APP_SUB_DOMAIN_DEPLOY') == 1){

  33.         foreach (C('APP_SUB_DOMAIN_RULES') as $key => $zlg) {            

  34.             if(false !== strstr($url,$zlg[0])){                

  35.                 $domain = $_SERVER['HTTP_HOST']; //获取主机域名

  36.                 $domain = $key.strstr($domain,'.'); //替换当前域名为分组域名

  37.                 $url = str_replace($zlg[0],'',$url); //去掉多余pathinfo里面的分组

  38.             }            

  39.         }

  40.     }

  41.     

  42.     

  43.     if(isset($host)) {

  44.        // $domain = $host.(strpos($host,'.')?'':strstr($_SERVER['HTTP_HOST'],'.'));

  45.         

  46.     }elseif($domain===true){

  47.         $domain = $_SERVER['HTTP_HOST'];

  48.         if(C('APP_SUB_DOMAIN_DEPLOY') ) { // 开启子域名部署

  49.             $domain = $domain=='localhost'?'localhost':'www'.strstr($_SERVER['HTTP_HOST'],'.');

  50.             // '子域名'=>array('项目[/分组]');

  51.             foreach (C('APP_SUB_DOMAIN_RULES') as $key => $rule) {

  52.                 if(false === strpos($key,'*') && 0=== strpos($url,$rule[0])) {

  53.                     $domain = $key.strstr($domain,'.'); // 生成对应子域名

  54.                     $url    =  substr_replace($url,'',0,strlen($rule[0]));

  55.                     break;

  56.                 }

  57.             }

  58.         }

  59.     }

  60.     // 解析参数

  61.     if(is_string($vars)) { // aaa=1&bbb=2 转换成数组

  62.         parse_str($vars,$vars);

  63.     }elseif(!is_array($vars)){

  64.         $vars = array();

  65.     }

  66.     if(isset($info['query'])) { // 解析地址里面参数 合并到vars

  67.         parse_str($info['query'],$params);

  68.         $vars = array_merge($params,$vars);

  69.     }

  70.     

  71.     // URL组装

  72.     $depr = C('URL_PATHINFO_DEPR');

  73.     if($url) {

  74.         if(0=== strpos($url,'/')) {// 定义路由

  75.             $route      =   true;

  76.             $url        =   substr($url,1);

  77.             if('/' != $depr) {

  78.                 $url    =   str_replace('/',$depr,$url);

  79.             }

  80.         }else{

  81.             if('/' != $depr) { // 安全替换

  82.                 $url    =   str_replace('/',$depr,$url);

  83.             }

  84.             // 解析分组、模块和操作

  85.             $url        =   trim($url,$depr);

  86.             $path       =   explode($depr,$url);

  87.             $var        =   array();

  88.             $var[C('VAR_ACTION')]       =   !empty($path)?array_pop($path):ACTION_NAME;

  89.             $var[C('VAR_MODULE')]       =   !empty($path)?array_pop($path):MODULE_NAME;

  90.             if($maps = C('URL_ACTION_MAP')) {

  91.                 if(isset($maps[strtolower($var[C('VAR_MODULE')])])) {

  92.                     $maps    =   $maps[strtolower($var[C('VAR_MODULE')])];

  93.                     if($action = array_search(strtolower($var[C('VAR_ACTION')]),$maps)){

  94.                         $var[C('VAR_ACTION')] = $action;

  95.                     }

  96.                 }

  97.             }

  98.             if($maps = C('URL_MODULE_MAP')) {

  99.                 if($module = array_search(strtolower($var[C('VAR_MODULE')]),$maps)){

  100.                     $var[C('VAR_MODULE')] = $module;

  101.                 }

  102.             }            

  103.             if(C('URL_CASE_INSENSITIVE')) {

  104.                 $var[C('VAR_MODULE')]   =   parse_name($var[C('VAR_MODULE')]);

  105.             }

  106.             if(!C('APP_SUB_DOMAIN_DEPLOY') && C('APP_GROUP_LIST')) {

  107.                 if(!empty($path)) {

  108.                     $group                  =   array_pop($path);

  109.                     $var[C('VAR_GROUP')]    =   $group;

  110.                 }else{

  111.                     if(GROUP_NAME != C('DEFAULT_GROUP')) {

  112.                         $var[C('VAR_GROUP')]=   GROUP_NAME;

  113.                     }

  114.                 }

  115.                 if(C('URL_CASE_INSENSITIVE') && isset($var[C('VAR_GROUP')])) {

  116.                     $var[C('VAR_GROUP')]    =  strtolower($var[C('VAR_GROUP')]);

  117.                 }

  118.             }

  119.         }

  120.     }

  121.     if(C('URL_MODEL') == 0) { // 普通模式URL转换

  122.         $url        =   __APP__.'?'.http_build_query(array_reverse($var));

  123.         if(!empty($vars)) {

  124.             $vars   =   urldecode(http_build_query($vars));

  125.             $url   .=   '&'.$vars;

  126.         }

  127.     }else{ // PATHINFO模式或者兼容URL模式

  128.         if(isset($route)) {

  129.             $url    =   __APP__.'/'.rtrim($url,$depr);

  130.         }else{

  131.             $url    =   __APP__.'/'.implode($depr,array_reverse($var));

  132.         }

  133.         if(!empty($vars)) { // 添加参数

  134.             foreach ($vars as $var => $val){

  135.                 if('' !== trim($val))   $url .= $depr . $var . $depr . urlencode($val);

  136.             } 

  137.             

  138.             

  139.             /*

  140.             *tp开启路由之后下一页路由失效问题 分页类URL组装

  141.             */

  142.             //如果开启路由

  143.             if(C('URL_ROUTER_ON')){                

  144.                 foreach (C('URL_ROUTE_RULES') as $zhaolg => $zlig){//遍历路由                        

  145.                     if(strstr('/'.GROUP_NAME.$url,$zlig)){//从网址中找路由规则

  146.                         $lg = strstr($zhaolg,'/:');//分析规则 带参数?

  147.                         if($lg){//如果规则带参数                            

  148.                             $tempzlg = str_replace('/:','',$lg);//取参数

  149.                             //dump($tempzlg);  

  150.                             if($tempzlg){

  151.                                 if(C('APP_SUB_DOMAIN_DEPLOY')){

  152.                                     $zlgurl = str_replace($zlig,$zhaolg,'/'.GROUP_NAME.$url); //url替换为路由规则 

  153.                                 }else{

  154.                                     $zlgurl = str_replace($zlig,$zhaolg,$url); //url替换为路由规则 

  155.                                 }                                

  156.                                 //注 /s/:c/c/4/p/__PAGE__

  157.                                 //dump($zlgurl);

  158.                                 $url = str_replace($lg.'/'.$tempzlg,'',$zlgurl);//url替换路由规则参数

  159.                                                             

  160.                             }    

  161.                         }else{//分析规则  不带参数

  162.                             if(C('APP_SUB_DOMAIN_DEPLOY')){

  163.                                 $url = str_replace($zlig,$zhaolg,'/'.GROUP_NAME.$url);//url替换为路由规则

  164.                             }else{

  165.                                 $url = str_replace($zlig,$zhaolg,$url);//url替换为路由规则 

  166.                             }                    

  167.                         }

  168.                     }

  169.                 }

  170.             }

  171.                          

  172.         }

  173.         if($suffix) {

  174.             $suffix   =  $suffix===true?C('URL_HTML_SUFFIX'):$suffix;

  175.             if($pos = strpos($suffix, '|')){

  176.                 $suffix = substr($suffix, 0, $pos);

  177.             }

  178.             if($suffix && '/' != substr($url,-1)){

  179.                 $url  .=  '.'.ltrim($suffix,'.');

  180.             }

  181.         }

  182.     }

  183.     if(isset($anchor)){

  184.         $url  .= '#'.$anchor;

  185.     }

  186.     if($domain) {

  187.         $url   =  (is_ssl()?'https://':'http://').$domain.$url;

  188.     }

  189.     if($redirect) // 直接跳转URL

  190.         redirect($url);

  191.     else

  192.         return $url;

  193.  }


跨域调用 比如现在是admin.baidu.com 使用U('/Home/aa/bb')之后,直接跳到home.baidu.com/aa/bb.html 
分页调用 还和以前一样

标签: Thinkphp通过U方法跨域

作者:良玉 分类:PHP 浏览:779 评论:0
留言列表
发表评论
来宾的头像