当前位置:首页>编程日记>正文

php中mvc控制器作用,理解PHP中的MVC编程之控制器

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

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

?module=login

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

module定义了使用哪一个模块,如users模块

class定义了使用哪一个功能类,如你想让用户login还是logout

event定义了使用哪一个具体事件

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

?module=users&class=login

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

以下是具体代码部分:

<?php

/**

* index.php

*

* @author Joe Stump <[email protected]>

* @copyright Joe Stump <[email protected]>

* @license

* @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.

* @param string $class Class name we're trying to load

* @return void

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'];

$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()) {

$result = $instance->$event();

if (!PEAR::isError($result)) {

$presenter = FR_Presenter::factory($instance->presenter,$instance);

if (!PEAR::isError($presenter)) {

$presenter->display();

die($presenter->getMessage());

} catch (Exception $error) {

die($error->getMessage());

die("You do not have access to the requested page!");

die("An valid module for your request was not found");

die("Could not find: $classFile");

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

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

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

# to /users/login/foo.html

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

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

Extending the Controller

相关报道:

print?//字符串转化为xml function toXmlDom(source){ var xmlDoc = null; if (window.ActiveXObject) { var ARR_ACTIVEX = [MSXML4.DOMDocument,MSXML3.DOMDocument,MSXML2.DOMDocument,MSXML.DOMDocument,Microsoft.XmlDom]; var XmlDomflag = false; for 更多

??ValidationType属性设置验证的类型,它可以是:DTD, XSD, XDR或者none。假如没有指定验证的类型(用ValidationType Auto选项),浏览器将主动的根据文档用最合 更多


http://www.coolblog.cn/news/435bef90bbd921f5.html

相关文章:

  • asp多表查询并显示_SpringBoot系列(五):SpringBoot整合Mybatis实现多表关联查询
  • s7day2学习记录
  • 【求锤得锤的故事】Redis锁从面试连环炮聊到神仙打架。
  • 矿Spring入门Demo
  • 拼音怎么写_老师:不会写的字用圈代替,看到孩子试卷,网友:人才
  • Linux 实时流量监测(iptraf中文图解)
  • Win10 + Python + GPU版MXNet + VS2015 + RTools + R配置
  • 美颜
  • shell访问php文件夹,Shell获取某目录下所有文件夹的名称
  • 如何优雅的实现 Spring Boot 接口参数加密解密?
  • LeCun亲授的深度学习入门课:从飞行器的发明到卷积神经网络
  • Mac原生Terminal快速登录ssh
  • java受保护的数据与_Javascript类定义语法,私有成员、受保护成员、静态成员等介绍...
  • mysql commit 机制_1024MySQL事物提交机制
  • 支撑微博千亿调用的轻量级RPC框架:Motan
  • jquery 使用小技巧
  • 2019-9
  • 法拉利虚拟学院2010 服务器,法拉利虚拟学院2010
  • vscode pylint 错误_将实际未错误的py库添加到pylint白名单
  • 科学计算工具NumPy(3):ndarray的元素处理
  • 工程师在工作电脑存 64G 不雅文件,被公司开除后索赔 41 万,结果…
  • linux批量创建用户和密码
  • newinsets用法java_Java XYPlot.setInsets方法代碼示例
  • js常用阻止冒泡事件
  • 气泡图在开源监控工具中的应用效果
  • 各类型土地利用图例_划重点!国土空间总体规划——土地利用
  • php 启动服务器监听
  • dubbo简单示例
  • 【设计模式】 模式PK:策略模式VS状态模式
  • [iptables]Redhat 7.2下使用iptables实现NAT
  • Ubuntu13.10:[3]如何开启SSH SERVER服务
  • CSS小技巧——CSS滚动条美化
  • JS实现-页面数据无限加载
  • 阿里巴巴分布式服务框架 Dubbo
  • 最新DOS大全
  • Django View(视图系统)
  • 阿里大鱼.net core 发送短信
  • 程序员入错行怎么办?
  • 两张超级大表join优化
  • 第九天函数
  • Linux软件安装-----apache安装
  • HDU 5988 最小费用流
  • Sorenson Capital:值得投资的 5 种 AI 技术
  • 《看透springmvc源码分析与实践》读书笔记一
  • 正式开课!如何学习相机模型与标定?(单目+双目+鱼眼+深度相机)
  • Arm芯片的新革命在缓缓上演
  • nagios自写插件—check_file
  • python3 错误 Max retries exceeded with url 解决方法
  • 行为模式之Template Method模式
  • 通过Spark进行ALS离线和Stream实时推荐