当前位置:K88软件开发文章中心网站服务器框架Yii 2.0 → 文章内容

响应(Responses)

减小字体 增大字体 作者:佚名  来源:网上搜集  发布时间:2019-1-26 15:28:42

由  创建, 最后一次修改 2016-02-24 响应当应用完成处理一个请求后, 会生成一个yii\web\Response响应对象并发送给终端用户 响应对象包含的信息有HTTP状态码,HTTP头和主体内容等, 网页应用开发的最终目的本质上就是根据不同的请求构建这些响应对象。在大多是情况下主要处理继承自 yii\web\Response 的?response?应用组件, 尽管如此,Yii也允许你创建你自己的响应对象并发送给终端用户,这方面后续会阐述。在本节,将会描述如何构建响应和发送给终端用户。状态码构建响应时,最先应做的是标识请求是否成功处理的状态,可通过设置 yii\web\Response::statusCode 属性,该属性使用一个有效的HTTP 状态码。例如,为标识处理已被处理成功, 可设置状态码为200,如下所示:Yii::$app->response->statusCode = 200;尽管如此,大多数情况下不需要明确设置状态码,因为 yii\web\Response::statusCode 状态码默认为200, 如果需要指定请求失败,可抛出对应的HTTP异常,如下所示:throw new \yii\web\NotFoundHttpException;当错误处理器?捕获到一个异常,会从异常中提取状态码并赋值到响应, 对于上述的 yii\web\NotFoundHttpException 对应HTTP 404状态码,以下为Yii预定义的HTTP异常:yii\web\BadRequestHttpException: status code 400.yii\web\ConflictHttpException: status code 409.yii\web\ForbiddenHttpException: status code 403.yii\web\GoneHttpException: status code 410.yii\web\MethodNotAllowedHttpException: status code 405.yii\web\NotAcceptableHttpException: status code 406.yii\web\NotFoundHttpException: status code 404.yii\web\ServerErrorHttpException: status code 500.yii\web\TooManyRequestsHttpException: status code 429.yii\web\UnauthorizedHttpException: status code 401.yii\web\UnsupportedMediaTypeHttpException: status code 415.如果想抛出的异常不在如上列表中,可创建一个yii\web\HttpException异常,带上状态码抛出,如下:throw new \yii\web\HttpException(402);HTTP 头部可在?response?组件中操控yii\web\Response::headers来发送HTTP头部信息,例如:$headers = Yii::$app->response->headers;// 增加一个 Pragma 头,已存在的Pragma 头不会被覆盖。$headers->add('Pragma', 'no-cache');// 设置一个Pragma 头. 任何已存在的Pragma 头都会被丢弃$headers->set('Pragma', 'no-cache');// 删除Pragma 头并返回删除的Pragma 头的值到数组$values = $headers->remove('Pragma');补充: 头名称是大小写敏感的,在yii\web\Response::send()方法调用前新注册的头信息并不会发送给用户。响应主体大多是响应应有一个主体存放你想要显示给终端用户的内容。如果已有格式化好的主体字符串,可赋值到响应的yii\web\Response::content属性,例如:Yii::$app->response->content = 'hello world!';如果在发送给终端用户之前需要格式化,应设置 yii\web\Response::format 和 yii\web\Response::data 属性,yii\web\Response::format 属性指定yii\web\Response::data中数据格式化后的样式,例如:$response = Yii::$app->response;$response->format = \yii\web\Response::FORMAT_JSON;$response->data = ['message' => 'hello world'];Yii支持以下可直接使用的格式,每个实现了yii\web\ResponseFormatterInterface 类, 可自定义这些格式器或通过配置yii\web\Response::formatters 属性来增加格式器。yii\web\Response::FORMAT_HTML: 通过 yii\web\HtmlResponseFormatter 来实现.yii\web\Response::FORMAT_XML: 通过 yii\web\XmlResponseFormatter来实现.yii\web\Response::FORMAT_JSON: 通过 yii\web\JsonResponseFormatter来实现.yii\web\Response::FORMAT_JSONP: 通过 yii\web\JsonResponseFormatter来实现.上述响应主体可明确地被设置,但是在大多数情况下是通过?操作?方法的返回值隐式地设置,常用场景如下所示:public function actionIndex(){ return $this->render('index');}上述的?index?操作返回?index?视图渲染结果,返回值会被?response?组件格式化后发送给终端用户。因为响应格式默认为yii\web\Response::FORMAT_HTML, 只需要在操作方法中返回一个字符串, 如果想使用其他响应格式,应在返回数据前先设置格式,例如:public function actionInfo(){ \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; return [ 'message' => 'hello world', 'code' => 100, ];}如上所述,触雷使用默认的?response?应用组件,也可创建自己的响应对象并发送给终端用户,可在操作方法中返回该响应对象,如下所示:public function actionInfo(){ return \Yii::createObject([ 'class' => 'yii\web\Response', 'format' => \yii\web\Response::FORMAT_JSON, 'data' => [ 'message' => 'hello world', 'code' => 100, ], ]);}注意: 如果创建你自己的响应对象,将不能在应用配置中设置?response?组件,尽管如此, 可使用?依赖注入?应用通用配置到你新的响应对象。浏览器跳转浏览器跳转依赖于发送一个Location?HTTP 头,因为该功能通常被使用,Yii提供对它提供了特别的支持。可调用yii\web\Response::redirect() 方法将用户浏览器跳转到一个URL地址,该方法设置合适的 带指定URL的?Location?头并返回它自己为响应对象,在操作的方法中,可调用缩写版yii\web\Controller::redirect(),例如:public function actionOld(){ return $this->redirect('http://example.com/new', 301);}在如上代码中,操作的方法返回redirect()?方法的结果,如前所述,操作的方法返回的响应对象会被当总响应发送给终端用户。除了操作方法外,可直接调用yii\web\Response::redirect() 再调用 yii\web\Response::send() 方法来确保没有其他内容追加到响应中。\Yii::$app->response->redirect('http://example.com/new', 301)->send();补充: yii\web\Response::redirect() 方法默认会设置响应状态码为302,该状态码会告诉浏览器请求的资源?临时?放

[1] [2]  下一页


响应(Responses)