理解PHP中的MVC编程之控制器
简单来讲,控制器的作用就是接受请求。它使用获取的方法,在这里是通过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
*
* @author joe stump <joe@joestump.net>
* @copyright joe stump <joe@joestump.net>
* @license http://www.opensource.org/licenses/gpl-license.php
* @package framework
*/
require_once(config.php);
// {{{ __autoload($class)
/**
* __autoload
*
* autoload is ran by php when it cant find a class it is trying to load.
* by naming our classes intelligently we should be able to load most classes
* dynamically.
*
* @author joe stump <joe@joestump.net>
* @param string $class class name were trying to load
* @return void
* @package framework
*/
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("requested module is not a valid framework module!");
}
$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("you do not have access to the requested page!");
}
} catch (exception $error) {
die($error->getmessage());
}
} else {
die("an valid module for your request was not found");
}
} else {
die("could not find: $classfile");
}
} else {
die("a valid module was not specified");
}
?>
接下来是以上代码具体的注释:
载入“config.php”
定义__autoload()函数。这是php5里面的一个新函数,方便动态地载入各个类。
如果一个argument被定义,那么载入相关的模块、类和具体事件
接下来就是一些判断以及错误的具体操作
最后一切无误后就载入表述层
【友好url】
如果你觉得上面例子讲到的请求url让你觉得不舒服的话,那么就用mod_rewrite来实现友好url吧。接下来是作者给这个框架写的实际重写标准代码:
rewriteengine on
# change the uri here to whatever you want your homepage to be
rewriterule ^/$ /index.php?module=welcome [l,qsa]
# changes /index.php?module=welcome to /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=login to /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]
extending the controller
【扩展控制器】
拥有一个集中控制器的一点好处就是你加入一些功能后,马上就能通过控制器体现出来。以下是几个可以扩展一下这个控制器的点子,使这个框架的整体能力更加强大:
你可以使用php5里一个新东西soapserver来自动检测一个请求是否为soap
你可以使用控制器来过滤所有的自动全局变量如$_get和$_post以防止恶意html代码等
你可以使用控制器即时地转换表述层,比如从默认的方式转到pdf方式
你可以直接在控制器中加入缓存机制,这样的好处是应用程序整体都能使用到缓存以提高效率
当然,需要注意一点的是,你在控制器中所增加的功能将体现在程序全局。如你想过滤所有的自动全局变量,但是很多应用程序的管理员需要使用到一些html代码,反而成为一件棘手的事情(译者注:本人的想法是可以加一个if条件语句,在加载特定模块时不应用过滤功能即可)。
- · 理解PHP中的MVC编程之MVC框架简介
- · SQL Server Express 数据库自动部署问题及解决
- · 用PHP文件上传的具体思路及实现
- · 回顾与展望PHP 5.0的变化与PHP 6.0展望
- · 一个产生中文累计数的代码片断
- · 在SQL Server 2005中解决死锁
- · 30分钟正则表达式指导
- · 不算不知道 44% 数据库开发者使用MySQL
- · 立即释放.net下的com组件
- · XHTML的目标,规则和细节
- · SQL Server 2005 提供的分页查询支持
- · ASP.NET程序中常用的三十三种代码
- · Sql server存储过程和C#分页类简化你的代码
- · SQL Server 2005新功能-TSQL
- · 在SQL Server 2005中编辑SQL Server 2000 DTS
- · .NET 连接到 Oracle的oci.dll加载错误解决方案
- · 如何在调用线程的时候传递参数
- · 专家预言:PHP将比Java更好更受欢迎
- · 在IIS6.0下ASP .NET 的版本冲突问题
- · 解决SqlTransaction用尽的问题(SQL处理超时)
- · 以前编写Like谓词被忽略的使用方法
- · 在编写存储过程时使用 Set NoCount On
- · ASP.NET 2.0运行时简要分析
- · .Net中如何操作IIS(原理篇)
- · 用.net开发不同操作系统下应用的winform的size大小问题
- · SQL Server数据库文件恢复技术
- · SQL SERVER中一些常见性能问题的总结
- · .NET下对二进制文件进行加密解密(C#)
- · 利用.NET的File控件上传文件的最终解决方案(个人版)
- · 如何把图片、声音等存储到sql中
- · MS-SQL server数据库开发精典技巧
- · 全文索引—CONTAINS语法
- · 获得所有表信息的SQL语句
- · .NET扫描远程计算机注册表
- · 利用JS获取IE客户端IP及MAC的实现
- · 简单而又复杂的ASP.NET编程模型
- · C#2.0终于有了?:便捷判断的单分支版
- · SQL Server 2005 Data Mining简介
- · ASP.NET控件开发之
- · ASP.NET中用哪种方式表格化数据
- · .NET平台下几种SOCKET模型的简要性能供参考
- · 使用 XML 模板 (MSSQL手册)
- · 使用带批注的 XDR 架构创建 XML 视图
- · 在XPath查询中指定轴(转自MSSQL手册)
- · JavaScript极速狂飙:CSS样式表的背景渲染效率
- · javascript应用:实现复选框全选/全不选切换
- · JavaScript极速狂飙:大容量字符型数组的快速检索
- · JavaScript极速狂飙:组合拼接字符串的效率
- · 图片自动缩小的js代码,用以防止图片撑破页面
- · PHP漏洞中的战争
- · automation服务器不能创建对象
- · Oracle数据库的备份及恢复策略研究
- · PHP程序加速探索之脚本执行速度测试
- · PHP中路径问题的解决方案
- · 学以致用 驳“ASP低能论”
- · 另类扩展名同样执行ASP
- · 语言决不是语法的不同之.NET 版本计算表达式的值
- · ASP中实现的类似URLEncode的编码函数及对应解码函数
- · 数据对象扩展成默认组件 PHP开发小组发布PHP5.1.0
- · ASP.NET 揭秘 ASP.NET页面的结构

