Cakephp 执行主要流程

yipeiwu_com6年前PHP代码库
加载基本文件
cake/basics.php 里面定义了常用的方法以及时间常量
$TIME_START = getMicrotime(); 记录开始执行时间
cake/config/paths.php 里面定义一些基本路径
cake/lib/object.php cake的基本类
cake/lib/inflector.php 这里主要是处理单复数,带下划开命名以及驼峰式命名
cake/lib/configure.php 里面提供文件配置的读写,路径的设置,以及加载文件的方法
cake/lib/cache.php 缓存的操作

Configure::getInstance(); 开始对项目的配置
config/core.php 项目的配置文件
config/bootstrap.php 项目的入口文件

App::import(‘Core', array(‘Dispatcher')); 加载核心,开始做正事了,GO
$Dispatcher = new Dispatcher();
$Dispatcher->dispatch($url); 开始执行,通过对当前的url解析,如果你设置了压缩Js、Css,则对这些文件压缩输出,如果你对页面设置缓存,则直接输出缓存页面,最后查找相应的Controller。如果找不到,则进行相应的错误处理。
实例化当前Controller,确定视图路径,实例化Component,获得仅当前Controller[不包含父类Controller]的方法
对当前Controller中私有方法、带admin路由或者带prefix的方法进行保护,不允许直接访问
设置当前Controller的基本属性,如base、here、webroot、plugin、params、action、 passedArgs[array_merge($this->params['pass'],$this->params['named'])]
调用Controller中的constructClasses方法
执行__mergeVars方法,该方法对父子类的components、helpers、uses等属性进行特殊合并处理
调用Component->init()方法,载入用户设置的系列components(Session为默认),并默认enabled属性为true。(该属性可以后期在beforeFilter里修改)
调用Component->initialize()方法,若系列components里有这个initialize方法并且该component 的enabled为true,则调用该components->initialize方法(这里enabled用户好像无法通过 Controller设置,只能为true)
调用当前Controller中beforeFilter()方法,这个方法是个好东西^_^
调用Component->startup()方法,同样,若系列components里有这个startup方法并且该component的 enabled为true,则调用该components->startup方法(这里enabled倒是可以通过beforeFilter设 置),该方法也是components里最重要的方法,比如Auth就在这里大作文章^_^
开始执行当前Controller里的Action方法
如果设置autoRender为true,则根据调用当前Controller的render()方法,否则返回或输出Action方法的返回的数据
调用Controller的render()方法时,先调用当前Controller中的beforeRender()方法
加载视图渲染类
调用Component->beforeRender()方法,同样,若系列components里有这个beforeRender方法并且该 component的enabled为true,则调用该components->beforeRender方法(这里enabled可以通过 beforeFilter设置)
获取当前Model的数据验证错误信息,给View使用
调用View的render()方法
载入相关Helper助手
调用Helper的beforeRender()方法
调用Helper的afterRender()方法
相关的缓存处理
执行renderLayout()方法,当然前提你要允许渲染布局,默认为default.ctp布局文件
调用Helper的beforeLayout()方法
调用Helper的afterLayout()方法
调用Component->shutdown()方法,同样,若系列components里有这个shutdown方法并且该component的 enabled为true,则调用该components->shutdown方法(这里enabled可以通过beforeFilter设置)
执行当前Controller里的afterFilter方法,这里你可以对视图的输出内容($controller->output)做一些处理
返回或输出视图数据。
流程完毕。

相关文章

PHP中获取变量的变量名的一段代码的bug分析

复制代码 代码如下: /** * 获取变量名 * * @param $string * @return $string * * $test = "helo"; * $test2 = "h...

php中opendir函数用法实例

本文实例分析了php中opendir函数用法。分享给大家供大家参考。具体如下: opendir语法:opendir(path,context) 目录,功能说明:打开目录句柄,opendi...

php弹出对话框实现重定向代码

1 利用js 实现 复制代码 代码如下: if(!$this->userInfo){ $alert_msg = "激活链接错误"; echo"<SCRIPT LANGUAGE...

PHP动态柱状图实现方法

PHP动态柱状图实现方法

本文实例讲述了PHP动态柱状图实现方法。分享给大家供大家参考。具体分析如下: 1.需求 查询最近一个月的数据总条数和审核通过的条数,做成柱状图 2.实现代码: <!DOCTY...

慎用preg_replace危险的/e修饰符(一句话后门常用)

preg_replace函数原型: mixed preg_replace ( mixed pattern, mixed replacement, mixed subject [, int...