首页 >php教程 >PHP源码 >框架url解析方法

框架url解析方法

PHP中文网
PHP中文网原创
2016-05-25 16:59:071092浏览

跳至

_module = '';
$this->_controller = C('defaultController');
$this->_action = C('defaultAction');
$this->_rules = C('urlManager=>rules');
}

    /**
*获取模型名称
**/
public function getModule()
{
return $this->_module;
}
    
/**
*获取控制器名称
**/
public function getController()
{
return $this->_controller;
}

    /**
*获取动作名称
**/
public function getAction()
{
return $this->_action;
}

    /**
*设置请求参数数组,解析二级域名
**/
    private function parse()
{
//先检查二级域名
$modules = C('modules');
if($modules) //只有设置了模块才检查
{
$host = $_SERVER['HTTP_HOST'];
if(C('domain') && $pos = strpos($host,C('domain')))
{
$preffix = substr($host,0,$pos-1);
if($pos = strrpos($preffix,'.'))
{
$preffix = substr($preffix,$pos);
}
if(in_array($preffix,$modules))
{
$this->_module = $preffix;
}
}
}
}



/**
*  解析普通参数
**/
private function parseParam($controllerObj)
{
$num=count($this->_requestParam);
if($num == 0)
return;
for($i=0,$num=count($this->_requestParam);$isetParam($this->_requestParam[$i],$this->_requestParam[$i+1]);
}
}

    /**
*  url解析规则
*  1. 'variable/varialbe:regexp>'=>'controller/action',
*  2. 'variable/variable'=>'controller/action',
*  3. 'variable/variable/:varialbe|regexp'=>'controller/action',
*  4. 'variable/:variable|regexp/varialbe'=>'controller/action',
*  5. 'variable/variable/*'=>'controller/action',
*  desc :开头的是变量,|后面的正则是变量匹配类型,如果没有,则是任意变量
**/
private function rule()
{
$parseParam = array();
$requestParamNum = count($this->_requestParam);
foreach($this->_rules as $rule=>$path)
{
$ruleParam = explode('/',$rule);
$ruleParamNum = count($ruleParam);
            if(($ruleParam[$ruleParamNum-1] == '*' && $requestParamNum >= $ruleParamNum-1) || $requestParamNum == $ruleParamNum)//rule满足初始条件
{
                foreach($ruleParam as $key=>$value)
{
if($value == '*')
{
break;
}
if(substr($value,0,1) == ':')
{
$value = substr($value,1);
    if(strpos($value,'|'))
    {
       list($value,$regexp)=explode('|',$value);
   if(!preg_match("/$regexp/",$this->_requestParam[$key]))
   {
                               $parseParam = array();
                               break;
   }
    }
                        $parseParam[] = $value; //参数名
    $parseParam[] = $this->_requestParam[$key]; //参数值
}
else if(strcasecmp($value,$this->_requestParam[$key]))
{
                         $parseParam = array();
  break;
}
}
if($parseParam)
{
$pathParam = explode('/',$path);
break;
}
}
}

if($parseParam && $key == $ruleParamNum-1) //匹配了
{
if($ruleParam[$ruleParamNum-1] == '*' && $requestParamNum >= $ruleParamNum)
{
                   for(;$key_requestParam[$key];
}
                $this->_requestParam = array_merge($pathParam,$parseParam);
unset($parseParam);
}
}

    /**
*  执行控制器方法
**/
public function run()
{
$this->parse();
        if($_SERVER['REQUEST_URI'])
        {
$this->_requestParam = explode('/',trim($_SERVER['REQUEST_URI'],'/'));
if($this->_rules)//存在路由规则
{
$this->rule(); //本次请求满足路由规则
}
if($modules && $this->_module == '' && in_array($this->_requestParam[0],$modules))
{
$this->_module = $this->_requestParam[0];
array_shift($this->_requestParam);
}
//print_r($this->_requestParam);
$controllerPath = APP_PATH.DS.($this->_module?'modules'.DS.$this->_module.DS:'').'controllers'.DS;
if(file_exists($controllerPath.$this->_requestParam[0].'Controller.class.php'))
{
                $this->_controller = $this->_requestParam[0];
array_shift($this->_requestParam);
}
}

$controllerClass = $this->_controller.'Controller';
        import($controllerClass,$controllerPath,'class');
$controllerObj = new $controllerClass($this);
    $this->_requestParam[0];

if($this->_requestParam[0] && method_exists($controllerObj,$this->_requestParam[0].'Action'))
{
            $this->_action = $this->_requestParam[0];
array_shift($this->_requestParam);
}
$this->parseParam($controllerObj); //对象时引用方式

try{
$action = $this->_action.'Action';
             $controllerObj->$action();
}
catch(Exception $e)
{
$this->rediretUrl('/');
}
}

    /**
*  url重定向
**/
public function rediret($url)
{
header('location:'.$url);
}

    /**
* 生成url方法,需要逆解析rule规则
**/
public function url($controller,$action,$param,$module = '')
{
         $urlPath = $controller.'/'.$action;
if($module)
             $urlPath = $module .'/'. $urlPath;
$parseParam = array();
if($this->_rules)
{
$ruleArr = array();
     foreach($this->_rules as $rule=>$path)
{
if($path ==  $urlPath)
{
$ruleArr[] = $rule;
}
}
if($ruleArr)
{
foreach($ruleArr as $rule)
{
                     $ruleParam = explode('/',$rule);
foreach($ruleParam as $key=>$value)
{
if(substr($value,0,1) == ':')
{
                             $value = substr($value,1);
if(strpos($value,'|'))
{
     list($value,$regexp)=explode('|',$value);
}
else
{
$regexp = '';
}
if(isset($param[$value]) && (!$regexp || preg_match("/$regexp/",$param[$value])))
{
                                 $parseParam[] = $param[$value];
unset($param[$value]);
}
else
{
                                 $parseParam = array();
break;
}
}
else
{
$parseParam[] = $value;
}
}
if($parseParam)
break;
} //foreach($ruleArr as $rule)
if($parseParam)
                    $urlPath = implode('/',$parseParam);
} //if($ruleArr)
} //if($this->_rules)
if($param)
{
foreach($param as $key=>$value)
{
                 $urlPath .= '/'.$key.'/'.$value;
}
}
return $urlPath;
}
}

                   

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn