ThinkPHP5.1源码,处理响应Response
\think\Container::get('app')->run()->send();
\think\Container::get('app')
返回thinkphp\library\think\App.php
的实例
APP.php
文件中的run()
方法,
通过中间件调用\thinkphp\library\think\route\Dispatch.php
的实例
返回 Response 对象
控制器执行是会调用 \thinkphp\library\think\route\dispatch\Controller.php
再调用 App.php
的 action()
方法,获取类名和方法名,再调用 invokeMethod()
方法反射执行
$response = $this->middleware->dispatch($this->request);
Dispatch.php
的autoResponse()
方法,判断控制器return
为空时,
执行ob_get_clean()
protected function autoResponse($data)
{
if ($data instanceof Response) {
$response = $data;
} elseif (!is_null($data)) {
// 默认自动识别响应输出类型
$isAjax = $this->request->isAjax();
$type = $isAjax ? $this->rule->getConfig('default_ajax_return') : $this->rule->getConfig('default_return_type');
$response = Response::create($data, $type);
} else {
$data = ob_get_clean();
$content = false === $data ? '' : $data;
$status = '' === $content && $this->request->isAjax() ? 204 : 200;
$response = Response::create($content, '', $status);
}
return $response;
}