phpmvc:经典的MVC模式在的PHP中的实现方法




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

延伸阅读

最新评论

发表评论