理解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 can@#t 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 we@#re 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");
}
?>
/**
* 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 can@#t 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 we@#re 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");
}
?>
[1] [2] 下一页
下一篇:Php 安全错误 Top 7
↓相关文章:
- · Php 安全错误 Top 7
- · PHP漏洞中的战争
- · 如何在十天内学会php之第十天
- · 如何在十天内学会php之第八天
- · 如何在十天内学会php之第七天
- · 如何在十天内学会php之第五天
- · 如何在十天学内会php之第三天
- · 如何在十天内学会php之第一天
- · php 命令行参数详解及应用
- · MySQL的管理工具:phpMyAdmin使用简介
- · 最令PHP初学者头痛的十四个问题
- · PHP教程.经验技巧
- · Smarty实例教学 实例篇
- · PHP 和 MySQL 基础教程(三)
- · PHP 和 MySQL 基础教程(一)
- · PHP中如何增加一个系统用户
- · 用XML+FSO+JS实现服务器端文件
- · Php5.0说明 ------为面向对象而生的php5
- · PHP面向对象编程快速入门
- · PHP设计聊天室步步通
- · 十天学会php之第九天
- · 十天学会php之第七天
- · 十天学会php之第五天
- · 十天学会php之第三天
- · 十天学会php之第一天
- · 王森:程序设计师忏悔录
- · 使用PHP4中的 IntegratedTemplate类实现BLOCK功能
- · 用PHP生成自己的LOG文件
- · PHP中的正规表达式(一)
- · 第十四节--命名空间 -- Classes and Objects in PHP5 [14]
- · 第十二节--类的自动加载 -- Classes and Objects in PHP5 [12]
- · 第十节--抽象方法和抽象类 -- Classes and Objects in PHP5 [10]
- · 第八节--访问方式 -- Classes and Objects in PHP5 [8]
- · 第六节--访问属性和方法 -- Classes and Objects in PHP5 [6]
- · 第四节--构造函数和析构函数 -- Classes and Objects in PHP5 [4]
- · 第二节--PHP5 的对象模型 -- Classes and Objects in PHP5 [2]
- · PHP:路在何方?
- · 初探 PHP5 (二)
- · 使用PHP实现动态网页
- · 用Php如何操作LDAP
- · PHP编码规范
- · PHP 简介
- · 用PEAR来写你的下一个php程序
- · 用PHP生成访客计数器
- · 浅谈PHP语法(三)

