You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In terms of validation/exception flow it would be very nice to be able to mark a route as json. Allowing Laravel to know that the intended route was json and should respond accordingly.
My use case:
We build an API for a user to consume.
User requests an API endpoint, but does not send valid JSON or Accept: application/json header
Laravel does not (by default) produce error, it just redirects to homepage with the error bag
User is confused what they are doing wrong
To fix this, currently the easiest way is to edit app\Exceptions\Handler.php and add some custom route detection to the shouldReturnJson() method. It would be nice if overriding core methods wasn't required, using some route logic.
I've built quite a few APIs with Laravel and I seem to run into this at least once per API.
e.g. Without the Accept header mentioned above, this would just redirect to the Laravel home page.
Route::prefix('api')
->group(function() {
Route::get('/', function() {
throw ValidationException::withMessages(['error' => 'This is a test']);
})
});
If we could add ->json() support, then the default Laravel Exception Handler could check this within shouldReturnJson() and honour it.
Route::prefix('api')
->json()
->group(function() {
Route::get('/', function() {
throw ValidationException::withMessages(['error' => 'This is a test']);
})
});
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
In terms of validation/exception flow it would be very nice to be able to mark a route as
json
. Allowing Laravel to know that the intended route was json and should respond accordingly.My use case:
Accept: application/json
headerTo fix this, currently the easiest way is to edit
app\Exceptions\Handler.php
and add some custom route detection to theshouldReturnJson()
method. It would be nice if overriding core methods wasn't required, using some route logic.I've built quite a few APIs with Laravel and I seem to run into this at least once per API.
e.g. Without the Accept header mentioned above, this would just redirect to the Laravel home page.
If we could add
->json()
support, then the default Laravel Exception Handler could check this withinshouldReturnJson()
and honour it.Beta Was this translation helpful? Give feedback.
All reactions