think\Response::header PHP Method

header() public method

设置响应头
public header ( string | array $name, string $value = null )
$name string | array 参数名
$value string 参数值
    public function header($name, $value = null)
    {
        if (is_array($name)) {
            $this->header = array_merge($this->header, $name);
        } else {
            $this->header[$name] = $value;
        }
        return $this;
    }

Usage Example

Example #1
0
 /**
  * @param Exception $exception
  * @return Response
  */
 protected function convertExceptionToResponse(Exception $exception)
 {
     // 收集异常数据
     if (App::$debug) {
         // 调试模式,获取详细的错误信息
         $data = ['name' => get_class($exception), 'file' => $exception->getFile(), 'line' => $exception->getLine(), 'message' => $this->getMessage($exception), 'trace' => $exception->getTrace(), 'code' => $this->getCode($exception), 'source' => $this->getSourceCode($exception), 'datas' => $this->getExtendData($exception), 'tables' => ['GET Data' => $_GET, 'POST Data' => $_POST, 'Files' => $_FILES, 'Cookies' => $_COOKIE, 'Session' => isset($_SESSION) ? $_SESSION : [], 'Server/Request Data' => $_SERVER, 'Environment Variables' => $_ENV, 'ThinkPHP Constants' => $this->getConst()]];
     } else {
         // 部署模式仅显示 Code 和 Message
         $data = ['code' => $this->getCode($exception), 'message' => $this->getMessage($exception)];
         if (!Config::get('show_error_msg')) {
             // 不显示详细错误信息
             $data['message'] = Config::get('error_message');
         }
     }
     //保留一层
     while (ob_get_level() > 1) {
         ob_end_clean();
     }
     $data['echo'] = ob_get_clean();
     ob_start();
     extract($data);
     include Config::get('exception_tmpl');
     // 获取并清空缓存
     $content = ob_get_clean();
     $response = new Response($content, 'html');
     if ($exception instanceof HttpException) {
         $statusCode = $exception->getStatusCode();
         $response->header($exception->getHeaders());
     }
     if (!isset($statusCode)) {
         $statusCode = 500;
     }
     $response->code($statusCode);
     return $response;
 }