【发布时间】:2015-10-09 01:33:58
【问题描述】:
我正在使用FormRequest 来验证从我的智能手机应用程序的 API 调用中发送的内容。所以,我希望 FormRequest 在验证失败时总是返回 json。
看到下面Laravel框架的源码,FormRequest的默认行为是reqeust是ajax或者wantJson返回json。
//Illuminate\Foundation\Http\FormRequest class
/**
* Get the proper failed validation response for the request.
*
* @param array $errors
* @return \Symfony\Component\HttpFoundation\Response
*/
public function response(array $errors)
{
if ($this->ajax() || $this->wantsJson()) {
return new JsonResponse($errors, 422);
}
return $this->redirector->to($this->getRedirectUrl())
->withInput($this->except($this->dontFlash))
->withErrors($errors, $this->errorBag);
}
我知道我可以在请求标头中添加Accept= application/json。 FormRequest 将返回 json。但我想提供一种更简单的方法来通过默认支持 json 来请求我的 API,而无需设置任何标头。所以,我试图在Illuminate\Foundation\Http\FormRequest 类中找到一些强制FormRequest 响应json 的选项。但我没有找到默认支持的任何选项。
解决方案 1:覆盖请求抽象类
我试图覆盖我的应用程序请求抽象类,如下所示:
<?php
namespace Laravel5Cg\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\JsonResponse;
abstract class Request extends FormRequest
{
/**
* Force response json type when validation fails
* @var bool
*/
protected $forceJsonResponse = false;
/**
* Get the proper failed validation response for the request.
*
* @param array $errors
* @return \Symfony\Component\HttpFoundation\Response
*/
public function response(array $errors)
{
if ($this->forceJsonResponse || $this->ajax() || $this->wantsJson()) {
return new JsonResponse($errors, 422);
}
return $this->redirector->to($this->getRedirectUrl())
->withInput($this->except($this->dontFlash))
->withErrors($errors, $this->errorBag);
}
}
如果我们需要强制响应json,我添加了protected $forceJsonResponse = false;。并且,在每个从 Request 抽象类扩展的 FormRequest 中。我设置了那个选项。
例如:我创建了一个 StoreBlogPostRequest 并为此 FormRequest 设置 $forceJsoResponse=true 并使其响应 json。
<?php
namespace Laravel5Cg\Http\Requests;
use Laravel5Cg\Http\Requests\Request;
class StoreBlogPostRequest extends Request
{
/**
* Force response json type when validation fails
* @var bool
*/
protected $forceJsonResponse = true;
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'title' => 'required|unique:posts|max:255',
'body' => 'required',
];
}
}
解决方案2:添加中间件并强制更改请求标头
我构建了一个中间件,如下所示:
namespace Laravel5Cg\Http\Middleware;
use Closure;
use Symfony\Component\HttpFoundation\HeaderBag;
class AddJsonAcceptHeader
{
/**
* Add Json HTTP_ACCEPT header for an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$request->server->set('HTTP_ACCEPT', 'application/json');
$request->headers = new HeaderBag($request->server->getHeaders());
return $next($request);
}
}
这是工作。但我想知道这个解决方案好吗?在这种情况下有什么 Laravel 方法可以帮助我吗?
【问题讨论】:
-
您好,只是一个建议,既然您要求默认响应类型,那么为什么不通过在您的句柄方法
$request->header('accept', 'application/json'); return $next($request);中添加一个中间件并将请求类型添加到 json 中,您有一个进行进一步扩展的地方,而不总是覆盖任何方法 -
谢谢!这是个好主意。我想。我会在上面的问题中更新这个实现
-
对不起。我试图设置 $request->header('Accept','application/json');在中间件中,但我发现我的请求具有默认的 Accept 标头“/”,因此我无法覆盖该 Accept 标头。我没有在请求中设置任何内容。
-
没关系,是否在标头中设置了默认接受值,中间件值将覆盖它
$request = $request->header('Accept','application/json'); return $next($request);我认为,请求没有被持久化。 -
我找到了覆盖请求头的方法,我们需要设置 $request->server 并重建 headerBag 如下: $request->server->set('HTTP_ACCEPT', 'application/json' ); $request->headers = new HeaderBag($request->server->getHeaders());
标签: php laravel laravel-5 laravel-validation