设为首页收藏本站language 语言切换
查看: 1454|回复: 0
收起左侧

MVC经典模式的PHP实现方法初探

[复制链接]
发表于 2010-2-24 13:37:59 | 显示全部楼层 |阅读模式
<>  作者:Harry Fuecks  翻译:Easy Chen  <BR><BR>  MVC模式在网站架构中十分常见。它允许我们建立一个三层结构的应用程式,从代码中分离出有用的层,帮助设计师和开发者协同工作以及提高我们维护和扩展既有程式的能力。  <BR>   <BR>  <BR>  视图(View)  <BR>  &#8220;视图&#8221;主要指我们送到Web浏览器的最终结果&#8212;&#8212;比如我们的脚本生成的HTML。当说到视图时,很多人想到的是模版,但是把模板方案叫做视图的正确性是值得怀疑的。  <BR>   <BR>  对视图来说,最重要的事情可能是它应该是&#8220;自我意识(self aware)&#8221;的,视图被渲染(render)时,视图的元素能意识到自己在更大框架中的角色。  <BR>   <BR>  以XML为例,可以说XML在被解析时,DOM API有着这样的认知&#8212;&#8212;一个DOM树里的节点知道它在哪里和它包含了什么。 (当一个XML文档中的节点用SAX解析时只有当解析到该节点时它才有意义。)  <BR>   <BR>  绝大多数模板方案使用简单的过程语言和这样的模板标签:  <BR>   <BR>    <BR>{some_text} </P><><BR>    <BR>{some_more_text} </P><><BR>   <BR>  它们在文档中没有意义,它们代表的意义只是PHP将用其他的东西来替换它。  <BR>   <BR>  如果你同意这种对视图的松散描述,你也就会同意绝大多数模板方案并没有有效的分离视图和模型。模板标签将被替换成什么存放在模型中。  <BR>   <BR>  在你实现视图时问自己几个问题:&#8220;全体视图的替换容易吗?&#8221;&#8220;实现一个新视图要多久?&#8221; &#8220;能很容易的替换视图的描述语言吗?(比如在同一个视图中用SOAP文档替换HTML文档)&#8221;  <BR>   <BR>  模型(Model)  <BR>  模型代表了程序逻辑。(在企业级程序中经常称为业务层(business layer))  <BR>   <BR>  总的来说,模型的任务是把原有数据转换成包含某些意义的数据,这些数据将被视图所显示。通常,模型将封装数据查询,可能通过一些抽象数据类(数据访问层)来实现查询。举例说,你希望计算英国年度降雨量(只是为了给你自己找个好点的度假地),模型将接收十年中每天的降雨量,计算出平均值,再传递给视图。  <BR>   <BR>  控制器(controller)  <BR>  简单的说控制器是Web应用中进入的HTTP请求最先调用的一部分。它检查收到的请求,比如一些GET变量,做出合适的反馈。在写出你的第一个控制器之前,你很难开始编写其他的PHP代码。最常见的用法是index.php中像switch语句的结构:  <BR>   <BR>    switch ($_GET[&#8217;viewpage&#8217;]) {  <BR>  case "news":  <BR>  $page=new NewsRenderer;  <BR>  break;  <BR>  case "links":  <BR>  $page=new LinksRenderer;  <BR>  break;  <BR>  default:  <BR>  $page=new HomePageRenderer;  <BR>  break;  <BR>  }  <BR>  $page-&gt;display();  <BR>  ?&gt;  <BR>   <BR>  这段代码混用了面向过程和对象的代码,但是对于小的站点来说,这通常是最好的选择。虽然上边的代码还可以优化。  <BR>   <BR>  控制器实际上是用来触发模型的数据和视图元素之间的绑定的控件。  <BR>   <BR>  例子  <BR>  这里是一个使用MVC模式的简单例子。  <BR>  首先我们需要一个数据库访问类,它是一个普通类。  <BR>   <BR>    /**  <BR>  * A simple class for querying MySQL  <BR>  */  <BR>  class DataAccess {  <BR>  /**  <BR>  * Private  <BR>  * $db stores a database resource  <BR>  */  <BR> var $db;  <BR>  /**  <BR>  * Private  <BR>  * $query stores a query resource  <BR>  */  <BR>  var $query; // Query resource  <BR>   <BR>  //! A constructor.  <BR>  /**  <BR>  * Constucts a new DataAccess object  <BR>  * @param $host string hostname for dbserver  <BR>  * @param $user string dbserver user  <BR>  * @param $pass string dbserver user password  <BR>  * @param $db string database name  <BR>  */  <BR>  function DataAccess ($host,$user,$pass,$db) {  <BR>  $this-&gt;db=mysql_pconnect($host,$user,$pass);  <BR>  mysql_select_db($db,$this-&gt;db);  <BR>  }  <BR>   <BR>  //! An accessor  <BR>  /**  <BR>  * Fetches a query resources_and_stores it in a local member  <BR>  * @param $sql string the database query to run  <BR>  * @return void  <BR>  */  <BR>  function fetch($sql) {  <BR>  $this-&gt;query=mysql_unbuffered_query($sql,$this-&gt;db); // Perform query here  <BR>  }  <BR>   <BR>  //! An accessor  <BR>  /**  <BR>  * Returns an associative array of a query row  <BR>  * @return mixed  <BR>  */  <BR>  function getRow () {  <BR>  if ( $row=mysql_fetch_array($this-&gt;query,MYSQL_ASSOC) )  <BR>  return $row;  <BR>  else  <BR>  return false;  <BR>  }  <BR>  }  <BR>  ?&gt;  <BR>   <BR>  在它上边放上模型。  <BR>    /**  <BR>  * Fetches "products" from the database  <BR>  */  <BR>  class ProductModel {  <BR>  /**  <BR>  * Private  <BR>  * $dao an instance of the DataAccess class  <BR>  */  <BR>  var $dao;  <BR>   <BR>  //! A constructor.  <BR>  /**  <BR>  * Constucts a new ProductModel object  <BR>  * @param $dbobject an instance of the DataAccess class  <BR>  */  <BR>  function ProductModel (&amp;$dao) {  <BR>  $this-&gt;dao=&amp; $dao;  <BR>  }  <BR>   <BR>  //! A manipulator  <BR>  /**  <BR>  * Tells the $dboject to store this query as a resource  <BR>  * @param $start the row to start from  <BR>  * @param $rows the number of rows to fetch  <BR>  * @return void  <BR>  */  <BR>  function listProducts($start=1,$rows=50) {  <BR>  $this-&gt;dao-&gt;fetch("SELECT * FROM products LIMIT ".$start.", ".$rows);  <BR> }  <BR>   <BR>  //! A manipulator  <BR>  /**  <BR>  * Tells the $dboject to store this query as a resource  <BR>  * @param $id a primary key for a row  <BR>  * @return void  <BR>  */  <BR>  function listProduct($id) {  <BR>  $this-&gt;dao-&gt;fetch("SELECT * FROM products WHERE PRODUCTID=&#8217;".$id."&#8217;");  <BR>  }  <BR>   <BR>  //! A manipulator  <BR>  /**  <BR>  * Fetches a product as an associative array from the $dbobject  <BR>  * @return mixed  <BR>  */  <BR>  function getProduct() {  <BR>  if ( $product=$this-&gt;dao-&gt;getRow() )  <BR>  return $product;  <BR>  else  <BR>  return false;  <BR>  }  <BR>  }  <BR>  ?&gt;  <BR>   <BR>  有一点要注意的是,在模型和数据访问类之间,它们的交互从不会多于一行&#8212;&#8212;没有多行被传送,那样会很快使程式慢下来。同样的程式对于使用模式的类,它只需要在内存中保留一行(Row)&#8212;&#8212;其他的交给已保存的查询资源(query resource)&#8212;&#8212;换句话说,我们让MYSQL替我们保持结果。  <BR>   <BR>  接下来是视图&#8212;&#8212;我去掉了HTML以节省空间,你可以查看这篇文章的完整代码。  <BR>   <BR>    /**  <BR>  * Binds product data to HTML rendering  <BR>  */  <BR>  class ProductView {  <BR>  /**  <BR>  * Private  <BR>  * $model an instance of the ProductModel class  <BR>  */  <BR>  var $model;  <BR>   <BR>  /**  <BR>  * Private  <BR>  * $output rendered HTML is stored here for display  <BR>  */  <BR>  var $output;  <BR>   <BR>  //! A constructor.  <BR>  /**  <BR>  * Constucts a new ProductView object  <BR>  * @param $model an instance of the ProductModel class  <BR>  */  <BR>  function ProductView (&amp;$model) {  <BR>  $this-&gt;model=&amp; $model;  <BR>  }  <BR>   <BR>  //! A manipulator  <BR>  /**  <BR>  * Builds the top of an HTML page  <BR>  * @return void  <BR>  */  <BR>  function header () {  <BR>   <BR>  }  <BR>   <BR>  //! A manipulator  <BR>  /**  <BR>  * Builds the bottom of an HTML page  <BR>  * @return void  <BR>  */  <BR>  function footer () {  <BR>   <BR>  }  <BR>   <BR>  //! A manipulator  <BR>  /**  <BR>  * Displays a single product  <BR>  * @return void  <BR>  */  <BR>  function productItem($id=1) {  <BR>  $this-&gt;model-&gt;listProduct($id);  <BR>  while ( $product=$this-&gt;model-&gt;getProduct() ) {  <BR> // Bind data to HTML  <BR>  }  <BR>  }  <BR>   <BR>  //! A manipulator  <BR>  /**  <BR>  * Builds a product table  <BR>  * @return void  <BR>  */  <BR>  function productTable($rownum=1) {  <BR>  $rowsperpage=&#8217;20&#8217;;  <BR>  $this-&gt;model-&gt;listProducts($rownum,$rowsperpage);  <BR>  while ( $product=$this-&gt;model-&gt;getProduct() ) {  <BR>  // Bind data to HTML  <BR>  }  <BR>  }  <BR>   <BR>  //! An accessor  <BR>  /**  <BR>  * Returns the rendered HTML  <BR>  * @return string  <BR>  */  <BR>  function display () {  <BR>  return $this-&gt;output;  <BR>  }  <BR>  }  <BR>  ?&gt;  <BR>   <BR>  最后是控制器,我们将把视图实现为一个子类。  <BR>   <BR>    /**  <BR>  * Controls the application  <BR>  */  <BR>  class ProductController extends ProductView {  <BR>   <BR>  //! A constructor.  <BR>  /**  <BR>  * Constucts a n </P>
您需要登录后才可以回帖 登录 | 论坛注册

本版积分规则

QQ|Archiver|手机版|小黑屋|sitemap|鸿鹄论坛 ( 京ICP备14027439号 )  

GMT+8, 2025-4-17 03:35 , Processed in 0.058944 second(s), 24 queries , Redis On.  

  Powered by Discuz!

  © 2001-2025 HH010.COM

快速回复 返回顶部 返回列表