MVC模式的PHP实现(3)
2003-10-20 10:52
856 查看
最后是控制器,我们将把视图实现为一个子类。
注意这不是实现MVC的唯一方式——比如你可以用控制器实现模型同时整合视图。这只是演示模式的一种方法。
我们的index.php 文件看起来像这样:
漂亮而简单。
我们有一些使用控制器的技巧,在PHP中你可以这样做:
一个建议是你最好定义程序URL的名字空间形式(namespace),那样它会比较规范比如:
通过它我们可以这样处理我们的控制器:
有时候,建立控制器是件很困难的事情,比如当你在开发速度和适应性之间权衡时。一个获得灵感的好去处是Apache group 的Java Struts,它的控制器完全是由XML文档定义的。
@ 完整程式
PS:本来是一个帖子的;可是死活贴不上来 -________-b 只好腰斩了
Part1 http://www.csdn.net/Develop/read_article.asp?id=21639
Part2 http://www.csdn.net/Develop/read_article.asp?id=21640
Part3 http://www.csdn.net/Develop/read_article.asp?id=21641
<?php /** * Controls the application */ class ProductController extends ProductView { //! A constructor. /** * Constucts a new ProductControllerobject * @param $model an instance of the ProductModel class * @param $getvars the incoming HTTP GET method variables */ function ProductController (& $model , $getvars = null ) { ProductView :: ProductView ( $model ); $this -> 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 (); } } ?> |
我们的index.php 文件看起来像这样:
<?php require_once( 'lib/DataAccess.php' ); require_once( 'lib/ProductModel.php' ); require_once( 'lib/ProductView.php' ); require_once( 'lib/ProductController.php' ); $dao =& new DataAccess ( 'localhost' , 'user' , 'pass' , 'dbname' ); $productModel =& new ProductModel ( $dao ); $productController =& new ProductController ( $productModel , $_GET ); echo $productController -> display (); ?> |
我们有一些使用控制器的技巧,在PHP中你可以这样做:
$this ->{ $_GET [ 'method' ]}( $_GET [ 'param' ]); |
"index.php?class=ProductView&method=productItem&id=4" |
$view =new $_GET [ 'class' ]; $view ->{ $_GET [ 'method' ]( $_GET [ 'id' ]); |
@ 完整程式
PS:本来是一个帖子的;可是死活贴不上来 -________-b 只好腰斩了
Part1 http://www.csdn.net/Develop/read_article.asp?id=21639
Part2 http://www.csdn.net/Develop/read_article.asp?id=21640
Part3 http://www.csdn.net/Develop/read_article.asp?id=21641
相关文章推荐
- MVC模式的PHP实现(2)
- MVC模式的PHP实现(1)
- smtp使用说明
- PHP 同UML类图
- PHP:路在何方?
- php程序对form提交的数据值的确定
- 用好Total Commander的十大替代功能
- PHP安全(2)
- PHP 安全 (1)
- pureftpd安装配置简明说明
- 高效的中文字符串截取函数
- FTP方式实现BS下的WORD操作
- Smartphone 2002中使用Web Service
- PHP中include()的使用问题
- 计算24点
- TManagedDataSet和DataSetPool的实现
- 用PHP即时添加,删除FTP帐号和更改FTP密码的函数
- WinCVS与CVSNT简明使用手则(三)
- Changes in PHP 5/Zend Engine 2.0
- Php 3.x与4.x中关于对象编程的不兼容问题之一 构造器说