您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

php实现mvc模式的例子

2024/2/27 22:36:08发布11次查看
{some_text}
{some_more_text}
复制代码
它们在文档中没有意义,它们代表的意义只是php将用其他的东西来替换它。
如果你同意这种对视图的松散描述,你也就会同意绝大多数模板方案并没有有效的分离视图和模型。模板标签将被替换成什么存放在模型中。
在你实现视图时问自己几个问题:“全体视图的替换容易吗?”“实现一个新视图要多久?” “能很容易的替换视图的描述语言吗?(比如在同一个视图中用soap文档替换html文档)”
二、模型(model)
模型代表了程序逻辑。(在企业级程序中经常称为业务层(business layer))
总的来说,模型的任务是把原有数据转换成包含某些意义的数据,这些数据将被视图所显示。通常,模型将封装数据查询,可能通过一些抽象数据类(数据访问层)来实现查询。举例说,你希望计算英国年度降雨量(只是为了给你自己找个好点的度假地),模型将接收十年中每天的降雨量,计算出平均值,再传递给视图。
三、控制器(controller)
简单的说控制器是web应用中进入的http请求最先调用的一部分。它检查收到的请求,比如一些get变量,做出合适的反馈。在写出你的第一个控制器之前,你很难开始编写其他的php代码。最常见的用法是index.php中像switch语句的结构:
display(); ?>
复制代码
这段代码混用了面向过程和对象的代码,但是对于小的站点来说,这通常是最好的选择。虽然上边的代码还可以优化。控制器实际上是用来触发模型的数据和视图元素之间的绑定的控件。
这里是一个使用mvc模式的简单例子。首先我们需要一个数据库访问类,它是一个普通类。
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 string the database query to run * @return void */ function fetch($sql) { $this->query=mysql_unbuffered_query($sql,$this->db); // perform query here }//! an accessor
/** * returns an associative array of a query row * @return mixed */ function getrow () { if ( $row=mysql_fetch_array($this->query,mysql_assoc) ) return $row; else return false; } } ?>
复制代码
在它上面放上模型。
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 * @return 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 * @return void */ function listproduct($id) { $this->dao->fetch(select * from products where productid='.$id.'); }//! a manipulator
/** * fetches a product as an associative array from the $dbobject * @return mixed */ function getproduct() { if ( $product=$this->dao->getrow() ) return $product; else return false; } } ?>
复制代码
注意:在模型和数据访问类之间,它们的交互从不会多于一行——没有多行被传送,那样会很快使程式慢下来。同样的程式对于使用模式的类,它只需要在内存中保留一行(row)——其他的交给已保存的查询资源(query resource)——换句话说,我们让mysql替我们保持结果。
接下来是视图(以下代码去掉了html内容)。
model=& $model; }//! a manipulator
/** * builds the top of an html page * @return void */ function header () {}
//! a manipulator
/** * builds the bottom of an html page * @return void */ function footer () {}
//! a manipulator
/** * displays a single product * @return void */ function productitem($id=1) { $this->model->listproduct($id); while ( $product=$this->model->getproduct() ) { // bind data to html } }//! a manipulator
/** * builds a product table * @return 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 * @return string */ function display () { return $this->output; } } ?>
复制代码
最后是控制器,我们将把视图实现为一个子类。
header(); switch ( $getvars['view'] ) { case product: $this->productitem($getvars['id']); break; default: if ( empty ($getvars['rownum']) ) { $this->producttable(); } else { $this->producttable($getvars['rownum']); } break; } $this->footer(); } } ?>
复制代码
注意:这不是实现mvc的唯一方式——比如你可以用控制器实现模型同时整合视图。这只是演示模式的一种方法。index.php 文件看起来像这样:
display(); ?>
复制代码
有一些使用控制器的技巧,在php中你可以这样做:$this->{$_get['method']}($_get['param']); 建议最好定义程序url的名字空间形式(namespace),那样它会比较规范比如:
index.php?class=productview&method=productitem&id=4
复制代码
通过它可以这样处理控制器:
$view=new $_get['class']; $view->{$_get['method']($_get['id']);
复制代码
有时建立控制器并非易事,比如需要在开发速度和适应性之间权衡时。一个获得灵感的好去处是apache group 的java struts,它的控制器完全是由xml文档定义的。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product