入口:index.php
获取地区
常规常量设置
引入application\classes\common\env文件获取开发环境
引入application\classes\common\area标记地区
引入 bootstrap 启动程序
引入Kohana_Core核心类
初始化程序
引入 kohana继承空类
设置时区、语言、自动加载方法
设置kohana初始选项
加载logs、config读取类
enable kohana模块
设置路由
此处通过set方法保存路由信息
public static function set($name, $uri = NULL, $regex = NULL)
{
return Route::$_routes[$name] = new Route($uri, $regex);
}
在request实例化时会读取路由信息进行匹配
执行request
echo Request::factory()
Creates a new request object for the given URI. New requests should be
created using the [Request::instance] or [Request::factory] methods.
1.获取请求配置信息
cli模式
http模式
SERVER_PROTOCOL、REQUEST_METHOD、HTTPS、HTTP_REFERER、HTTP_USER_AGENT、HTTP_X_REQUESTED_WITH、HTTP_X_FORWARDED_FOR
查询http协议中是否设定,未设定则取项目默认值
2.Request::$initial = $request = new Request($uri, $cache);
实例化Request
3.设置protocol 默认为 http/1.1 并保存 get->query post->post 数据
设置安全、请求方法、referrer、body
execute()
* Processes the request, executing the controller action that handles this
* request, determined by the [Route].
*
* 1. Before the controller action is called, the [Controller::before] method
* will be called.
* 2. Next the controller action will be called.
* 3. After the controller action is called, the [Controller::after] method
* will be called.
通过 request_client 实例调用excute方法,创建一个response实例,然后通过request_client_internal调用excute_request方法
通过反射类来调用控制器中的方法返回response,同时在excute_request中计算方法耗时和内存消耗
关键代码:
// Load the controller using reflection
$class = new ReflectionClass($prefix.$controller);
// Create a new instance of the controller
$controller = $class->newInstance($request, $response);
// Run the controller's execute() method
$response = $class->getMethod('execute')->invoke($controller);
执行excute时,会先调用before(),再执行action,最后执行 after()
最后执行response回调函数(暂时未深入了解用法)
send_headers()
Sends the response status and all set headers.
主要设置协议头部返回信息
body(); echo 最后的内容 生命周期结束
if ($content === NULL)
{
// Act as a getter
return $this->_body;
}
// Act as a setter
$this->_body = $content;
return $this;