如何在Laravel 5.1中强制FormRequest返回json?

0pizxfdo  于 2022-11-18  发布在  其他
关注(0)|答案(6)|浏览(154)

我正在使用FormRequest来验证从我的智能手机应用程序发送的API调用。因此,我希望FormRequest在验证失败时总是返回json。
我看了下面的Laravel框架的源代码,如果reqeust是 AJAX 或wantJson,FormRequest的默认行为是return 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。但我想提供一种更简单的方法来请求我的API,即在默认情况下支持json,而无需设置任何标头。因此,我尝试在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);
    }
}

我添加了protected $forceJsonResponse = false;来设置是否需要强制响应json。并且,在每个从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);
    }
}

这是工作。但我想知道这个解决方案好吗?在这种情况下有没有拉拉维尔的方法来帮助我?

fafcakar

fafcakar1#

这让我很困惑,为什么在Laravel中这么难做。最后,基于你覆盖Request类的想法,我想出了这个。
app/Http/Requests/ApiRequest.php

<?php

namespace App\Http\Requests;

class ApiRequest extends Request
{
    public function wantsJson()
    {
        return true;
    }
}

然后,在每个控制器中只传递\App\Http\Requests\ApiRequest
public function index(ApiRequest $request)

svmlkihl

svmlkihl2#

我知道这篇文章有点老了,但是我刚刚做了一个中间件,它用“application/json”替换了请求的“Accept”头。这使得wantsJson()函数在使用时返回true。(这在Laravel 5.2中测试过,但是我认为它在5.1中的工作原理是一样的)
以下是实现该功能的方法:
1.创建文件app/Http/Middleware/Jsonify.php

namespace App\Http\Middleware;

use Closure;

class Jsonify
{

    /**
     * Change the Request headers to accept "application/json" first
     * in order to make the wantsJson() function return true
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * 
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $request->headers->set('Accept', 'application/json');

        return $next($request);
    }
}

1.将中间件添加到app/Http/Kernel.php文件的$routeMiddleware表中

protected $routeMiddleware = [
    'auth'       => \App\Http\Middleware\Authenticate::class,
    'guest'      => \App\Http\Middleware\RedirectIfAuthenticated::class,
    'jsonify'    => \App\Http\Middleware\Jsonify::class
];

1.最后在routes.php中使用它,就像使用任何中间件一样。

Route::group(['prefix' => 'api/v1', 'middleware' => ['jsonify']], function() {
    // Routes
});
2fjabf4q

2fjabf4q3#

基于ZeroOne's response,如果您正在使用Form Request验证,则可以覆盖failedValidation方法,以便在验证失败时始终返回json。
这个解决方案的好处是,你不会覆盖所有的响应来返回json,而只是覆盖验证失败。所以对于所有其他的PHP异常,你仍然会看到友好的Laravel错误页面。

namespace App\Http\Requests;

use Illuminate\Contracts\Validation\Validator;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Exceptions\HttpResponseException;
use Symfony\Component\HttpFoundation\Response;

class InventoryRequest extends FormRequest
{
    protected function failedValidation(Validator $validator)
    {
        throw new HttpResponseException(response($validator->errors(), Response::HTTP_UNPROCESSABLE_ENTITY));
    }
}
lf3rwulv

lf3rwulv4#

如果您的请求具有X-Request-With:XMLHttpRequest头或接受内容类型为application/jsonFormRequest将自动返回包含状态为422的错误的json响应。

eqqqjvef

eqqqjvef5#

我只是覆盖了failedValidation函数

protected function failedValidation(Validator $validator)
{
    if ($this->wantsJson()) {
        throw new HttpResponseException(
            Response::error(__('api.validation_error'), 
            $validator->errors(), 
            470, 
            [], 
            new ValidationException)
        );
    }

    parent::failedValidation($validator);
}

所以我的自定义输出示例如下:

{
    "error": true,
    "message": "Validation Error",
    "reference": [
        "The device id field is required.",
        "The os version field is required.",
        "The apps version field is required."
    ],
}

BTW响应::laravel中不存在错误。我正在使用macroable创建新方法

Response::macro('error', function ($msg = 'Something went wrong', $reference = null, $code = 400, array $headers = [], $exception = null) {
      return response()->json(//custom here);
 });
k7fdbhmy

k7fdbhmy6#

我得出这个解(Laravel 9):

throw new ValidationException(
    $validator,
    new JsonResponse([
        'errors' => $validator->errors()->messages(),
    ], 422),
);

相关问题