本文章来讲述一下关于mvc中controller类教程,通过上两节我们知道 程序通过单一入口文件的route类决定了 唯一的moudle, conttoller, action,并在最后执行了
代码如下 | 复制代码 |
$route->run(); /** * 执行相应的 MCA * */ private function run () { $filePath = APPLICATION_PATH.'/controller/'.$this->_moudle.'/'.$this->_conttoller.'.inc.php'; $isNo = 0; if(file_exists($filePath)) { include "$filePath"; $controller_tp = $this->_conttoller.'Controller'; $controller = new $controller_tp;
if (method_exists($controller,$this->_action.'Action')) { $acion_tmp = $this->_action.'Action'; $controller->$acion_tmp(); }else { $isNo = 1; }
}else { $isNo = 1; }
if ($isNo) { $filePath = APPLICATION_PATH.'/controller/default/index.inc.php'; $this->_moudle = $this->_default['module']; $this->_conttoller = $this->_default['conttoller']; $this->_action = $this->_default['action'];
($this->_moudle != $this->_default['module']) && include "$filePath"; $controller = new indexController; $controller->indexAction(); } }
|
当相关'Controller'文件存在时执行
代码如下 | 复制代码 |
include "$filePath"; $controller_tp = $this->_conttoller.'Controller'; $controller = new $controller_tp; |
上述三行代码的意思是,根据确定好的 conttoller 包含相应文件,并实例化相应的conttoller。
代码如下 | 复制代码 |
$acion_tmp = $this->_action.'Action'; $controller->$acion_tmp(); |
根据相应的Action 执行相应的action
所有的 Controller 类都集成一个公用的Controller 类,本节课我们就来分析一下公共的Controller 类
/**
* 前台公共类 接口
* 实现公共部分代码
*/
/**
* 本文件只能被index。php包含
*/
defined("WEB_AUTH") || die("NO_AUTH");
/**
* 包含菜单配置文件
*/
代码如下 | 复制代码 |
class Controller { public $tpl; public $controller; public $body;//右边菜单 public $_route ; public $html_; public $tpl_;
/* * 构造函数 */ public function __construct() { $this->init(); }
/* * 初始化变量,顶部菜单和模板 */ protected function init() { global $TPL,$route; $this->tpl = $TPL; $this->_route = $route; }
/** * 模板变量传第 */ protected function diplayTpl() { $this->body || $this->body = $this->_route->getActionName(); $this->tpl->assign("body",$this->body); /*设置本控制器的模板目录*/ $this->controller ||$this->controller =$this->_route->getControllerName(); $this->tpl->assign("controller",$this->controller); $this->tpl->display($this->layout); } /** * smarty封装类 * @param string $name * @param string $value */ public function assign($name,$value) { $this->tpl->assign($name,$value); }
/** * 显示另外的模板 * @param string $name * @param string $value */ protected function displayOther($file) { $this->assign("otherTpl",TRUE); $this->tpl->display($file); } /** * 显示某个MCA的body模板 * 0=>m 1=>c =>a */ protected function getMcaBody($array) { return 'http://www.cnblogs.com/../'.$array[0].'/body/'.$array[1].'/'.$array[2]; } /* * 析构函数,显示页面 */ protected function __destruct() { $this->tpl->_tpl_vars['otherTpl'] || $this->diplayTpl(); } /** * 中途退出 */ protected function _exit($msg = "") { $this->assign("otherTpl",TRUE); die($msg); }
/** * 用 $this->html_var=value放法给变量赋值 * 用 $this->tpl_var=value放法给变量赋值 */ protected function __set($name,$value) { if(strtolower(substr($name,0,5)) == "html_" || strtolower(substr($name,0,4)) == "tpl_") { $this->assign(substr($name,5),$value); } } } ?> |
首先看
代码如下 | 复制代码 |
protected function __destruct() { $this->tpl->_tpl_vars['otherTpl'] || $this->diplayTpl(); } |
这是所有Controller 类 生命周期结束时候要执行的函数(搜索一下php魔术方法 查看详情)
本框架利用这时候解析模板,这样的好处是,当Controller中相关执行完相关数据处理,后自动执行相关的模板(View);而不用每次在程序最后调用模板
代码如下 | 复制代码 |
protected function __set($name,$value) { if(strtolower(substr($name,0,5)) == "html_" || strtolower(substr($name,0,4)) == "tpl_") { $this->assign(substr($name,5),$value); } } |
这个函数简化了程序向模板传递变量的方法,以smarty为例,在程序中需要执行 $tpl->assign(‘key’,$value);
来向模板中注册变量,而此函数中简化了此方法 ,只需 $this->html_key=$value;来实现相同的作用.(利用开发环境的提示功能,在前面声明
代码如下 | 复制代码 |
public $html_; public $tpl_; |

使用数据库存储会话的主要优势包括持久性、可扩展性和安全性。1.持久性:即使服务器重启,会话数据也能保持不变。2.可扩展性:适用于分布式系统,确保会话数据在多服务器间同步。3.安全性:数据库提供加密存储,保护敏感信息。

在PHP中实现自定义会话处理可以通过实现SessionHandlerInterface接口来完成。具体步骤包括:1)创建实现SessionHandlerInterface的类,如CustomSessionHandler;2)重写接口中的方法(如open,close,read,write,destroy,gc)来定义会话数据的生命周期和存储方式;3)在PHP脚本中注册自定义会话处理器并启动会话。这样可以将数据存储在MySQL、Redis等介质中,提升性能、安全性和可扩展性。

SessionID是网络应用程序中用来跟踪用户会话状态的机制。1.它是一个随机生成的字符串,用于在用户与服务器之间的多次交互中保持用户的身份信息。2.服务器生成并通过cookie或URL参数发送给客户端,帮助在用户的多次请求中识别和关联这些请求。3.生成通常使用随机算法保证唯一性和不可预测性。4.在实际开发中,可以使用内存数据库如Redis来存储session数据,提升性能和安全性。

在无状态环境如API中管理会话可以通过使用JWT或cookies来实现。1.JWT适合无状态和可扩展性,但大数据时体积大。2.Cookies更传统且易实现,但需谨慎配置以确保安全性。

要保护应用免受与会话相关的XSS攻击,需采取以下措施:1.设置HttpOnly和Secure标志保护会话cookie。2.对所有用户输入进行输出编码。3.实施内容安全策略(CSP)限制脚本来源。通过这些策略,可以有效防护会话相关的XSS攻击,确保用户数据安全。

优化PHP会话性能的方法包括:1.延迟会话启动,2.使用数据库存储会话,3.压缩会话数据,4.管理会话生命周期,5.实现会话共享。这些策略能显着提升应用在高并发环境下的效率。

thesession.gc_maxlifetimesettinginphpdeterminesthelifespanofsessiondata,setInSeconds.1)它'sconfiguredinphp.iniorviaini_set().2)abalanceIsiseededeedeedeedeedeedeedto to to avoidperformance andununununununexpectedLogOgouts.3)

在PHP中,可以使用session_name()函数配置会话名称。具体步骤如下:1.使用session_name()函数设置会话名称,例如session_name("my_session")。2.在设置会话名称后,调用session_start()启动会话。配置会话名称可以避免多应用间的会话数据冲突,并增强安全性,但需注意会话名称的唯一性、安全性、长度和设置时机。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

Atom编辑器mac版下载
最流行的的开源编辑器

禅工作室 13.0.1
功能强大的PHP集成开发环境