I am trying to get a json only out for api routes in Laravel 11.
<?php
use AppHttpMiddlewareForceJsonResponse;
use IlluminateFoundationApplication;
use IlluminateFoundationConfigurationExceptions;
use IlluminateFoundationConfigurationMiddleware;
return Application::configure(basePath: dirname(__DIR__))
->withRouting(
web: __DIR__ . '/../routes/web.php',
api: __DIR__ . '/../routes/api.php',
commands: __DIR__ . '/../routes/console.php',
health: '/up',
)
->withMiddleware(function (Middleware $middleware) {
$middleware->api(append: [
ForceJsonResponse::class,
]);
})
->withExceptions(function (Exceptions $exceptions) {
})->create();
Just using append() works,
->withMiddleware(function (Middleware $middleware) {
$middleware->append([
ForceJsonResponse::class,
]);
})
This is applicable for all routes, which i dont want.
I would like to know why the $middleware->api(append: []); is not working.
I am trying to get json output for the api routes in Laravel.