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

Chu*_*ung 23 php laravel laravel-5 laravel-validation

我正在使用 FormRequest来验证从我的智能手机应用程序的API调用中发送的内容.所以,我希望FormRequest在验证失败时总是返回json.

我看到了Laravel框架的以下源代码,如果reqeust是Ajax或wantJson,则FormRequest的默认行为是返回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);
}
Run Code Online (Sandbox Code Playgroud)

我知道我可以添加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);
    }
}
Run Code Online (Sandbox Code Playgroud)

我添加protected $forceJsonResponse = false;到设置是否需要强制响应json.并且,在每个FormRequest中,它从Request抽象类扩展而来.我设定了这个选项.

例如:我创建了一个StoreBlogPostRequest并$forceJsoResponse=true为此FormRequest 设置并使其响应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',
        ];
    }
}
Run Code Online (Sandbox Code Playgroud)

解决方案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);
    }
}
Run Code Online (Sandbox Code Playgroud)

这是工作.但我想知道这个解决方案好吗?在这种情况下,有没有Laravel Way可以帮助我?

Bou*_*egh 30

让我难以理解为什么在Laravel这么难做.最后,根据你的想法覆盖Request类,我想出了这个.

app/Http/Requests/ApiRequest.php

<?php

namespace App\Http\Requests;


class ApiRequest extends Request
{
    public function wantsJson()
    {
        return true;
    }
}
Run Code Online (Sandbox Code Playgroud)

然后,在每个控制器中通过 \App\Http\Requests\ApiRequest

public function index(ApiRequest $request)

  • 接受:标题中的application/json也可能对此有所帮助. (4认同)

Sim*_*hin 29

我知道这篇文章很老但我只是制作了一个中间件,用"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);
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  2. 将中间件添加到文件$routeMiddleware表中app/Http/Kernel.php

    protected $routeMiddleware = [
        'auth'       => \App\Http\Middleware\Authenticate::class,
        'guest'      => \App\Http\Middleware\RedirectIfAuthenticated::class,
        'jsonify'    => \App\Http\Middleware\Jsonify::class
    ];
    
    Run Code Online (Sandbox Code Playgroud)
  3. 最后在你的routes.php任何中间件中使用它.就我而言,它看起来像这样:

    Route::group(['prefix' => 'api/v1', 'middleware' => ['jsonify']], function() {
        // Routes
    });
    
    Run Code Online (Sandbox Code Playgroud)


T30*_*T30 9

根据ZeroOne 的响应,如果您使用表单请求验证,您可以重写 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));
    }
}
Run Code Online (Sandbox Code Playgroud)