我们这里介绍PHP实现MVC的几种方法,首先将从什么是MVC,然后以代码的形式介绍PHP实现MVC。相信所有习惯了使用MVC来组织自己的项目的人,让他选择一个新的项目构架时,一定不会放弃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。
如何PHP实现MVC?
下面是一个超级简单的MVC结构实现,甚至连数据源都用了一个内置的固定数组,虽然简单,但其实众多的PHP Framework核心实现的思想应该和这个是差不多的,只不过一些framework提供了更多的方便开发者使用的工具,我也想自己来实现一个PHP的框架,目前正在着手策划中,也希望自己能够从框架的开发中学习到更多的PHP设计思想和方法。
<ol class="dp-xml"> <li class="alt"><span><span>Controller.php </span></span></li> <li class=""><span> </span></li> <li class="alt"><span>include 'Model.php'; </span></li> <li class=""><span>include 'View.php'; </span></li> <li class="alt"><span>class Controller { </span></li> <li class=""> <span>private $</span><span class="attribute"><font color="#ff0000">model</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">''</font></span><span>; </span> </li> <li class="alt"> <span>private $</span><span class="attribute"><font color="#ff0000">view</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">''</font></span><span>; </span> </li> <li class=""><span> </span></li> <li class="alt"><span>public function Controller(){ </span></li> <li class=""> <span>$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span class="attribute"><font color="#ff0000">model</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">new</font></span><span> Model(); </span> </li> <li class="alt"> <span>$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span class="attribute"><font color="#ff0000">view</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">new</font></span><span> View(); </span> </li> <li class=""><span>} </span></li> <li class="alt"><span> </span></li> <li class=""> <span>public function doAction( $</span><span class="attribute"><font color="#ff0000">method</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">'defaultMethod'</font></span><span>, $</span><span class="attribute"><font color="#ff0000">params</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">array</font></span><span>() ){ </span> </li> <li class="alt"><span>if( empty($method) ){ </span></li> <li class=""> <span>$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>defaultMethod(); </span> </li> <li class="alt"><span>}else if( method_exists($this, $method) ){ </span></li> <li class=""><span>call_user_func(array($this, $method), $params); </span></li> <li class="alt"><span>}else{ </span></li> <li class=""> <span>$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>nonexisting_method(); </span> </li> <li class="alt"><span>} </span></li> <li class=""><span>} </span></li> <li class="alt"><span> </span></li> <li class=""> <span>public function link_page($</span><span class="attribute"><font color="#ff0000">name</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">''</font></span><span>){ </span> </li> <li class="alt"> <span>$</span><span class="attribute"><font color="#ff0000">links</font></span><span> = $this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>model-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>getLinks(); </span> </li> <li class=""> <span>$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>view-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>display($links); </span> </li> <li class="alt"><span> </span></li> <li class=""> <span>$</span><span class="attribute"><font color="#ff0000">result</font></span><span> = $this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>model-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>getResult($name); </span> </li> <li class="alt"> <span>$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>view-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>display($result); </span> </li> <li class=""><span>} </span></li> <li class="alt"><span> </span></li> <li class=""><span>public function defaultMethod(){ </span></li> <li class="alt"> <span>$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>br(); </span> </li> <li class=""><span>echo "This is the default method. "; </span></li> <li class="alt"><span>} </span></li> <li class=""><span> </span></li> <li class="alt"><span>public function nonexisting_method(){ </span></li> <li class=""> <span>$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>br(); </span> </li> <li class="alt"><span>echo "This is the noexisting method. "; </span></li> <li class=""><span>} </span></li> <li class="alt"><span> </span></li> <li class=""><span>public function br(){ </span></li> <li class="alt"> <span>echo "</span><strong><font color="#006699"><span class="tag"><span class="tag-name">br</span></span></font></strong><span> </span><span class="tag"><strong><font color="#006699">/></font></strong></span><span>"; </span> </li> <li class=""><span>} </span></li> <li class="alt"><span>} </span></li> <li class=""> <span>$</span><span class="attribute"><font color="#ff0000">controller</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">new</font></span><span> Controller(); </span> </li> <li class="alt"> <span>$controller-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>doAction('link_page', 'b'); </span> </li> <li class=""> <span>$controller-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>doAction(); </span> </li> <li class="alt"><span> </span></li> <li class=""><span>Model.php </span></li> <li class="alt"><span> </span></li> <li class=""><span>Code </span></li> <li class="alt"><span>class Model { </span></li> <li class=""> <span>private $</span><span class="attribute"><font color="#ff0000">database</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">array</font></span><span>( </span> </li> <li class="alt"> <span>"a" =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> "hello world", </span> </li> <li class=""> <span>"b" =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> "ok well done", </span> </li> <li class="alt"> <span>"c" =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> "good bye", </span> </li> <li class=""><span>); </span></li> <li class="alt"><span> </span></li> <li class=""><span>//@TODO connect the database </span></li> <li class="alt"><span> </span></li> <li class=""><span>//run the query and get the result </span></li> <li class="alt"><span> public function getResult($name){ </span></li> <li class=""><span>if( empty($name) ){ </span></li> <li class="alt"><span>return FALSE; </span></li> <li class=""><span>} </span></li> <li class="alt"><span> </span></li> <li class=""> <span>if( in_array($name, array_keys( $this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>database ) ) ){ </span> </li> <li class="alt"> <span>return $this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>database[$name]; </span> </li> <li class=""><span>} </span></li> <li class="alt"><span>} </span></li> <li class=""><span>public function getLinks(){ </span></li> <li class="alt"> <span>$</span><span class="attribute"><font color="#ff0000">links</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">"<a>Link A</a> "</font></span><span>; </span> </li> <li class=""> <span>$</span><span class="attribute"><font color="#ff0000">links.</font></span><span>= </span><span class="attribute-value"><font color="#0000ff">"<a>Link B</a> "</font></span><span>; </span> </li> <li class="alt"> <span>$</span><span class="attribute"><font color="#ff0000">links.</font></span><span>= </span><span class="attribute-value"><font color="#0000ff">"<a>Link C</a> "</font></span><span>; </span> </li> <li class=""><span> </span></li> <li class="alt"><span>return $links; </span></li> <li class=""><span>} </span></li> <li class="alt"><span>} </span></li> <li class=""><span> </span></li> <li class="alt"><span>View.php </span></li> <li class=""><span> </span></li> <li class="alt"><span>class View { </span></li> <li class=""><span> </span></li> <li class="alt"><span>public function display($output){ </span></li> <li class=""><span>// ob_start(); </span></li> <li class="alt"><span> </span></li> <li class=""><span>echo $output; </span></li> <li class="alt"><span>} </span></li> <li class=""><span>} </span></li> </ol>P>