改进controller类的success error result 方法增加header参数

This commit is contained in:
thinkphp
2016-08-10 12:11:12 +08:00
parent c35ee2da86
commit b76e9a4190

View File

@@ -31,9 +31,10 @@ trait Jump
* @param string $url 跳转的URL地址 * @param string $url 跳转的URL地址
* @param mixed $data 返回的数据 * @param mixed $data 返回的数据
* @param integer $wait 跳转等待时间 * @param integer $wait 跳转等待时间
* @param array $header 发送的Header信息
* @return void * @return void
*/ */
protected function success($msg = '', $url = null, $data = '', $wait = 3) protected function success($msg = '', $url = null, $data = '', $wait = 3, array $header = [])
{ {
$code = 1; $code = 1;
if (is_numeric($msg)) { if (is_numeric($msg)) {
@@ -58,7 +59,7 @@ trait Jump
$result = ViewTemplate::instance(Config::get('template'), Config::get('view_replace_str')) $result = ViewTemplate::instance(Config::get('template'), Config::get('view_replace_str'))
->fetch(Config::get('dispatch_success_tmpl'), $result); ->fetch(Config::get('dispatch_success_tmpl'), $result);
} }
$response = Response::create($result, $type); $response = Response::create($result, $type)->header($header);
throw new HttpResponseException($response); throw new HttpResponseException($response);
} }
@@ -69,9 +70,10 @@ trait Jump
* @param string $url 跳转的URL地址 * @param string $url 跳转的URL地址
* @param mixed $data 返回的数据 * @param mixed $data 返回的数据
* @param integer $wait 跳转等待时间 * @param integer $wait 跳转等待时间
* @param array $header 发送的Header信息
* @return void * @return void
*/ */
protected function error($msg = '', $url = null, $data = '', $wait = 3) protected function error($msg = '', $url = null, $data = '', $wait = 3, array $header = [])
{ {
$code = 0; $code = 0;
if (is_numeric($msg)) { if (is_numeric($msg)) {
@@ -96,7 +98,7 @@ trait Jump
$result = ViewTemplate::instance(Config::get('template'), Config::get('view_replace_str')) $result = ViewTemplate::instance(Config::get('template'), Config::get('view_replace_str'))
->fetch(Config::get('dispatch_error_tmpl'), $result); ->fetch(Config::get('dispatch_error_tmpl'), $result);
} }
$response = Response::create($result, $type); $response = Response::create($result, $type)->header($header);
throw new HttpResponseException($response); throw new HttpResponseException($response);
} }
@@ -107,9 +109,10 @@ trait Jump
* @param integer $code 返回的code * @param integer $code 返回的code
* @param mixed $msg 提示信息 * @param mixed $msg 提示信息
* @param string $type 返回数据格式 * @param string $type 返回数据格式
* @param array $header 发送的Header信息
* @return void * @return void
*/ */
protected function result($data, $code = 0, $msg = '', $type = '') protected function result($data, $code = 0, $msg = '', $type = '', array $header = [])
{ {
$result = [ $result = [
'code' => $code, 'code' => $code,
@@ -118,7 +121,7 @@ trait Jump
'data' => $data, 'data' => $data,
]; ];
$type = $type ?: $this->getResponseType(); $type = $type ?: $this->getResponseType();
$response = Response::create($result, $type); $response = Response::create($result, $type)->header($header);
throw new HttpResponseException($response); throw new HttpResponseException($response);
} }