How to force FormRequest return json in Laravel 5.1?
It boggles my mind why this is so hard to do in Laravel. In the end, based on your idea to override the Request class, I came up with this.
app/Http/Requests/ApiRequest.php
<?phpnamespace App\Http\Requests;class ApiRequest extends Request{ public function wantsJson() { return true; }}
Then, in every controller just pass \App\Http\Requests\ApiRequest
public function index(ApiRequest $request)
I know this post is kind of old but I just made a Middleware that replaces the "Accept" header of the request with "application/json". This makes the wantsJson()
function return true
when used. (This was tested in Laravel 5.2 but I think it works the same in 5.1)
Here's how you implement that :
Create the file
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); }}
Add the middleware to your
$routeMiddleware
table of yourapp/Http/Kernel.php
fileprotected $routeMiddleware = [ 'auth' => \App\Http\Middleware\Authenticate::class, 'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class, 'jsonify' => \App\Http\Middleware\Jsonify::class];
Finally use it in your
routes.php
as you would with any middleware. In my case it looks like this :Route::group(['prefix' => 'api/v1', 'middleware' => ['jsonify']], function() { // Routes});
Based on ZeroOne's response, if you're using Form Request validation you can override the failedValidation method to always return json in case of validation failure.
The good thing about this solution, is that you're not overriding all the responses to return json, but just the validation failures. So for all the other Php exceptions you'll still see the friendly Laravel error page.
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)); }}