-
-
Save meSingh/c90c5abc81a9cc0687a62a84eb2c696b to your computer and use it in GitHub Desktop.
<?php | |
namespace App\Http\Middleware; | |
use Closure; | |
use Illuminate\Http\Request; | |
class JsonMiddleware | |
{ | |
public function handle(Request $request, Closure $next) | |
{ | |
$request->headers->set('Accept', 'application/json'); | |
return $next($request); | |
} | |
} | |
Hi, thank you so much!
I noticed something. When a NotFoundHttpException happens because of sending a model id that doesn't exist via path parameter, the error is not displayed in JSON (unless you manually add the header). In the next code you can see how I solved it:
app/Exceptions/Handler.php
public function render($request, Exception $exception)
{
if (in_array('api', $request->route()->middleware())) {
$request->headers->set('Accept', 'application/json');
}
return parent::render($request, $exception);
}
My workaround doesn't work in Laravel 5.4.
Yours works in Laravel 5.4 only when the validation is not using a Form Request.
It seems this middleware only works when it is a global middleware. When moved into the web
or api
routes, it does not seem to work.
It seems this middleware only works when it is a global middleware. When moved into the
web
orapi
routes, it does not seem to work.
You have to add it as first middleware in the api group
thanks!
You can place that middleware wherever you want in your middleware groups, just make sure that it's defined first in the $middlewarePriority
array.
protected $middlewarePriority = [
\App\Http\Middleware\JsonOnlyMiddleware::class, // <- Make it the top priority middleware
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\Illuminate\Contracts\Auth\Middleware\AuthenticatesRequests::class,
\Illuminate\Routing\Middleware\ThrottleRequests::class,
\Illuminate\Session\Middleware\AuthenticateSession::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
\Illuminate\Auth\Middleware\Authorize::class,
];
If you cannot see that array in your Http Kernel, copy/paste it from the parent class: Illuminate\Foundation\Http\Kernel
. (I think it's been removed from 7.x by default).
Hi, thank you so much!
I noticed something. When a NotFoundHttpException happens because of sending a model id that doesn't exist via path parameter, the error is not displayed in JSON (unless you manually add the header). In the next code you can see how I solved it:app/Exceptions/Handler.php
public function render($request, Exception $exception) { if (in_array('api', $request->route()->middleware())) { $request->headers->set('Accept', 'application/json'); } return parent::render($request, $exception); }
When it's a 404 error (route not found) there will be an exception, because $request->route()
will return null.
I just change it a little:
if ($request->route() !== null && in_array('api', $request->route()->middleware())) {
$request->headers->set('Accept', 'application/json');
}
But with this fix you'll get an HTML response as default in case when it's 404 error.
If you need 404 also in JSON, update the render function as follows. This solved for me in Laravel 6
app/Exceptions/Handler.php
public function render($request, Exception $exception)
{
if (substr($request->server()['REQUEST_URI'],1,3) =='api') {
$request->headers->set('Accept', 'application/json');
}
return parent::render($request, $exception);
}
Returns "text/html; charset=UTF-8"
Route::get('/foo', function () {
return 'hello world';
});
Originally shared by Alexander Lichter
https://twitter.com/TheAlexLichter/status/969879256271597568