本身寫的路由和視圖類

<?php
/**
*url解析分發類
**/
class Route{
    private $_module;
private $_controller;
private $_action;
private $_requestParam = array();
private $_rules = array();

public function __construct(){
    $this->_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);$i<$num;$i+=2)
{
            $controllerObj->setParam($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<$requestParamNum;$key++)
   $parseParam[] = $this->_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;
}

} php

View.class.php
<?php /** *視圖文件 * **/ class View { private $_variables = array();//參數列表 private $_useLayout = true;//使用公共魔板 private $_templateType = 'phtml'; private $_route; private $_layout; public function __construct($route) { $this->_route = $route; $this->_layout = APP_PATH.DS.'layouts'.DS.'main.'.$this->_templateType; } public function __set($name,$value) { $this->_variables[$name] = $value; } public function __get($name) { return $this->_variables[$name]; } public function noLayout() {         $this->_useLayout = false; } public function setTemplateType($templatType) {         $this->_templateType = $templatType; }      public function setLayout($layout,$path='') {        $path == '' && $path = APP_PATH.DS.'layouts';        $this->_layout = $path.DS.$layout.'.'.$this->_templateType; } public function render($template = '') {         if(!$template)            $template = $this->_route->getAction(); $module = $this->_route->getModule(); $templateFile = APP_PATH.DS.($module?'modules'.DS.$module.DS:'').'views'.DS.$this->_route->getController().DS.$template.'.'.$this->_templateType; $this->_variables && extract($this->_variables,EXTR_OVERWRITE); if($this->_useLayout) { include $this->_layout; } else { include $templateFile; } } }
相關文章
相關標籤/搜索