浅解用PHP实现MVC

什么是MVC?

关于MVC的定义和解释,能够说多种多样。咱们能够在Wiki或者[2]中找到更为详尽的解释,这里我并不打算,也没有能力进行深刻的讲解,从PHP开发的角度来说,MVC能够归纳为:


视图(The View):

一说到视图,咱们不少人都会想到模板引擎(诸如Smarty等等)。其实就是各类各样的输出,好比说html模板和Javascript文件等。

模块(The Model)

模块表明了程序的逻辑,在企业应用中一般称为业务逻辑层。通常来说,这一层完成的工做是把原始的数据处理成按照咱们设计的数据结构存储的有意义的数据序列,并将这些数据交给视图去处理。一般状况下, 模块中会利用一个数据抽象类来进行与数据操做有关的处理。
Model一般包含了哪些用来同数据库打交道的函数。

控制器(The Controller)

控制器使全部WEB应用的第一站,他接受收到的参数,好比$_GET变量,而后作出相应的反应。

关于MVC的是否适合PHP的争论也不少,人们不断的讨论MVC是否适合PHP[3],如今也已经有了不少的MVC框架,诸如PHP MVC Frameworks中列出的[4]。那么,为何人们这么热衷于MVC,咱们为何要使用MVC在咱们的设计中呢。

为何用MVC?

MVC最先是用来解决桌面GUI的编程问题,最先的MVC框架应该是Sun在1999年提出的Model 2,后来演变成了Struts。MVC带给人们深入的印象,可是咱们在使用的过程当中,却并无认真的想过为何使用MVC。
在传统的桌面应用中,一旦Model中有时间发生,咱们能够主动的让View界面进行刷新,从而展现后台发生的变化。而在Web应用中,咱们彷佛限于传统的Http的Request/Response的方式,咱们彷佛没有办法让用户端进行更新。这一段讨论,并非认为MVC不可以用来进行WEB应用的开发,而是以为从某种程度上来讲,他还不是最合适的。
关于使用MVC的争论还有不少[1],可是相信全部习惯了使用MVC来组织本身的项目的人,让他选择一个新的项目构架时,必定不会放弃MVC。

如何实现MVC?

下面是一个超级简单的MVC结构实现,甚至连数据源都用了一个内置的固定数组,虽然简单,但其实众多的PHP Framework核心实现的思想应该和这个是差很少的,只不过一些framework提供了更多的方便开发者使用的工具,我也想本身来实现一个PHP的框架,目前正在着手策划中,也但愿本身可以从框架的开发中学习到更多的PHP设计思想和方法。php

Controller.phphtml

 include 'Model.php';
include 'View.php';
class Controller {
    private $model     = '';
    private $view     = '';
    
    public function Controller(){
        $this->model    =    new Model();
        $this->view        =    new View();
    }
    
    public function doAction( $method = 'defaultMethod', $params = array() ){
        if( empty($method) ){
            $this->defaultMethod();
        }else if( method_exists($this, $method) ){
            call_user_func(array($this, $method), $params);
        }else{
            $this->nonexisting_method();
        }
    }
    
    public function link_page($name = ''){
        $links = $this->model->getLinks();
        $this->view->display($links);
        
        $result = $this->model->getResult($name);
        $this->view->display($result);
    }
    
    public function defaultMethod(){
        $this->br();
        echo "This is the default method. ";
    }
    
    public function nonexisting_method(){
        $this->br();
        echo "This is the noexisting method. ";
    }
    
    public function br(){
        echo "<br />";
    }
}

$controller = new Controller();
$controller->doAction('link_page', 'b');
$controller->doAction();

Model.phpnode

class Model {
    private $database = array(
        "a"    =>    "hello world",
        "b"    =>    "ok well done",
        "c"    =>    "good bye",
    );
    
    //@TODO connect the database
    
    //run the query and get the result
    public function getResult($name){
        if( empty($name) ){
            return FALSE;
        }
        
        if( in_array($name, array_keys( $this->database ) ) ){
            return $this->database[$name];
        }
    }

    public function getLinks(){
        $links = "<a href='#'>Link A</a>&nbsp;&nbsp;";
        $links.= "<a href='#'>Link B</a>&nbsp;&nbsp;";
        $links.= "<a href='#'>Link C</a>&nbsp;&nbsp;";
        
        return $links;
    }
}

View.phpweb

 class View {
    
    public function display($output){
//        ob_start();
        
        echo $output;
    }
    
}

参考资料:
一、MVC and Web apps            http://www.sitepoint.com/blogs/2005/12/22/mvc-and-web-apps-oil-and-water/
二、Model View Controller        http://www.phpwact.org/pattern/model_view_controller
三、Is MVC over designed Crap    http://phplens.com/phpeverywhere/?q=node/view/143
四、PHP MVC Framworks            http://www.phpwact.org/php/mvc_frameworks
五、Model 2                    http://en.wikipedia.org/wiki/Model_2
六、Stupid Easy MVC            http://codesnipers.com/?q=node/158&&title=Stupidly-Easy-MVC-in-PHP-or-数据库

相关文章
相关标签/搜索