MVC框架-前端控制器-原理

<?php
/**
 * @name :前端控制器.front.php
 * @author :PHPCQ.COM
 *
 */
class FrontController{
    protected 
$_controller,$_action,$_params,$_body;
    static 
$_instance;
    public static function 
getInstance(){
        if (!(
self::$_instance instanceof  self)) {
            
self::$_instance = new self();
        }
        return 
self::$_instance;
    }
    private function 
__construct(){
        
$request $_SERVER['REQUEST_URI'];
        
$splits explode('/',trim($request,'/'));
        
$this->_controller = !empty($splits[0])?$splits[0]:'index';
        
$this->_action = !empty($splits[1])?$splits[1]:'index';
        if (!empty(
$splits[2])) {
            
$keys $values = array();
            for (
$index 2,$cnt count($splits);$idx $cnt;$idx++){
                if (
$idx == 0) {
                    
$keys[] = $splits[$idx];
                } else {
                    
$values[] = $splits[$idx];
                }
            }
            
$this->_params array_combine($keys,$values);
            
//print_r($this->_params);
        
}
    }
    public function 
route(){
        if (
class_exists($this->getController())) {
            
$rc = new ReflectionClass($this->getController());
            if (
$rc->implementsInterface('IController')) {
                if (
$rc->hasMethod($this->getAction())) {
                    
$controller $rc->newInstance();
                    
$method $rc->getMethod($this->getAction());
                    
$method->invoke($controller);
                } else {
                    throw new 
Exception("Action");
                }
            } else {
                throw new 
Exception("Interface");
            } 
        } else {
            throw new 
Exception("Controller");
        }
    }
    public function 
getParams(){
        return 
$this->_params;
    }
    public function 
getController(){
        return 
$this->_controller;
    }
    public function 
getAction(){
        return 
$this->_action;
    }
    public function 
getBody(){
        return 
$this->_body;
    }
    public function 
setBody($body){
        
$this->_body $body;
    }
}      

关于PHP博客

PHP blogger

博客-致力于分享PHP技术的开发心得, 记录站长生活的点点滴滴,PHP博客采用PHP 面向对象编程+Smarty模板引擎+Mysql数据库 开发(全站纯静态化与伪静态化)
QQ:334192009
电邮:admin@PHPdesigner.org

本站搜索

本站最新文章

博客最近档案

友情链接