当前位置: 首页 > 图文教程 > 网络编程 > PHP > 理解PHP中的MVC编程之控制器

PHP
php sprintf()函数让你的sql操作更安全
php SQLite学习笔记与常见问题分析
使用PHP socke 向指定页面提交数据
jq的get传参数在utf-8中乱码问题的解决php版
PHP 得到根目录的 __FILE__ 常量
PHP 表单提交给自己
PHP4中session登录页面的应用
简单示例AJAX结合PHP代码实现登录效果代码
php+mysql写的简单留言本实例代码
php在线打包程序源码
php intval的测试代码发现问题
php5编程中的异常处理详细方法介绍
php include的妙用,实现路径加密
PHP中$_SERVER的详细参数与说明
php 全文搜索和替换的实现代码
PHP一些常用的正则表达式字符的一些转换
php自动跳转中英文页面
MySql中正则表达式的使用方法描述
说明的比较细的php 正则学习实例
新安装的MySQL数据库需要注意的安全知识

理解PHP中的MVC编程之控制器


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-08-14   浏览: 244 ::
收藏到网摘: n/a

简单来讲,控制器的作用就是接受请求。它使用获取的方法,在这里是通过URI,载入一个功能模块来刷新或者提交一个表述层。控制器将使用$_GET自动全局变量来判断载入哪一个模块。

一个请求的例子,看起来像这样:

http://example.com/index.php?module=login

这看起来很简单,但是在实现的过程中却不是。这里是几个控制器能识别的argument部分:

module定义了使用哪一个模块,如users模块
class定义了使用哪一个功能类,如你想让用户login还是logout
event定义了使用哪一个具体事件

这样一个更复杂的例子可以解释上面的各个argument最终组成的请求URL:

http://example.com/index.php?module=users&class=login

这段请求告诉控制器应该载入users模块,然后是login类,最后因为没有定义具体事件,所以运行login::__default()默认事件。

以下是具体代码部分:

<?php
 /**
*index.php
*
*@authorJoeStump<[email protected]
*@copyrightJoeStump<[email protected]
*@licensehttp://www.opensource.org/licenses/gpl-license.php
*@packageFramework
 */

 require_once('config.php');

 //{{{__autoload($class)
 /**
*__autoload
*
*AutoloadisranbyPHPwhenitcan'tfindaclassitistryingtoload.
*Bynamingourclassesintelligentlyweshouldbeabletoloadmostclasses
*dynamically.
*
*@authorJoeStump<[email protected]
*@paramstring$classClassnamewe'retryingtoload
*@returnvoid
*@packageFramework
 */

 function__autoload($class)
 {
$file=str_replace('_','/',substr($class,2)).'.php';
require_once(FR_BASE_PATH.'/includes/'.$file);
 }
 //}}}

 if(isset($_GET['module'])){
$module=$_GET['module'];
if(isset($_GET['event'])){
 $event=$_GET['event'];
}else{
 $event='__default';
}

 if(isset($_GET['class'])){
$class=$_GET['class'];
 }else{
$class=$module;
 }

 $classFile=FR_BASE_PATH.'/modules/'.$module.'/'.$class.'.php';
 if(file_exists($classFile)){
require_once($classFile);
if(class_exists($class)){
 try{
$instance=new$class();
if(!FR_Module::isValid($instance)){
 die("Requestedmoduleisnotavalidframeworkmodule!");
}

$instance->moduleName=$module;
if($instance->authenticate()){
 try{
$result=$instance->$event();
if(!PEAR::isError($result)){
 $presenter=FR_Presenter::factory($instance->presenter,$instance);

if(!PEAR::isError($presenter)){
 $presenter->display();
}else{
 die($presenter->getMessage());
}
 }
}catch(Exception$error){
 die($error->getMessage());
}
 }else{
die("Youdonothaveaccesstotherequestedpage!");
 }
}catch(Exception$error){
 die($error->getMessage());
}
 }else{
die("Anvalidmoduleforyourrequestwasnotfound");
 }
 }else{
die("Couldnotfind:$classFile");
 }
 }else{
die("Avalidmodulewasnotspecified");
}

?>
接下来是以上代码具体的注释:

载入“config.php”

定义__autoload()函数。这是PHP5里面的一个新函数,方便动态地载入各个类。

如果一个argument被定义,那么载入相关的模块、类和具体事件

接下来就是一些判断以及错误的具体操作

最后一切无误后就载入表述层

【友好URL】

如果你觉得上面例子讲到的请求URL让你觉得不舒服的话,那么就用mod_rewrite来实现友好URL吧。接下来是作者给这个框架写的实际重写标准代码:

RewriteEngineOn

#ChangetheURIheretowhateveryouwantyourhomepagetobe

RewriteRule^/$/index.php?module=welcome[L,QSA]

#Changes/index.php?module=welcometo/welcome

RewriteCond%{DOCUMENT_ROOT}/%{REQUEST_FILENAME}!-d

RewriteCond%{DOCUMENT_ROOT}/%{REQUEST_FILENAME}!-f

RewriteRule^/([^/]*)$/index.php?module=$1[L,QSA]

#Changes/index.php?module=users&class=loginto/users/login

RewriteCond%{DOCUMENT_ROOT}/%{REQUEST_FILENAME}!-d

RewriteCond%{DOCUMENT_ROOT}/%{REQUEST_FILENAME}!-f

RewriteRule^/([^/]*)/([^/]*)$/index.php?module=$1&class=$2[L,QSA]

#Changes/index.php?module=users&class=login&event=foo

#to/users/login/foo.html

RewriteCond%{DOCUMENT_ROOT}/%{REQUEST_FILENAME}!-d

RewriteCond%{DOCUMENT_ROOT}/%{REQUEST_FILENAME}!-f

RewriteRule^/([^/]*)/([^/]*)/([^/]*).html$\

/index.php?module=$1&class=$2&event=$3[L,QSA]

ExtendingtheController
【扩展控制器】

拥有一个集中控制器的一点好处就是你加入一些功能后,马上就能通过控制器体现出来。以下是几个可以扩展一下这个控制器的点子,使这个框架的整体能力更加强大:
 
你可以使用PHP5里一个新东西SoapServer来自动检测一个请求是否为SOAP

你可以使用控制器来过滤所有的自动全局变量如$_GET和$_POST以防止恶意HTML代码等

你可以使用控制器即时地转换表述层,比如从默认的方式转到PDF方式

你可以直接在控制器中加入缓存机制,这样的好处是应用程序整体都能使用到缓存以提高效率

当然,需要注意一点的是,你在控制器中所增加的功能将体现在程序全局。如你想过滤所有的自动全局变量,但是很多应用程序的管理员需要使用到一些HTML代码,反而成为一件棘手的事情(译者注:本人的想法是可以加一个if条件语句,在加载特定模块时不应用过滤功能即可)。