In laravel 11 and 12, making new routes files and applying middleware and domain to them difficult or impossible?
In Laravel 11 and 12, making routes custom files and applying middleware, add domain to route files is difficult; may be it is possible for some or few developers, I am trying to add middleware to just api or just api.php for specific domain with specific custom middleware for few day, but I could not apply the custom middleware to routes files and also applying the domain to particular route file. It was quite easy in laravel 8, 9 or 10. Laravel framework getting harder for such small adjustment or adding such bit thing, I could not understand.
<?php
use App\Http\Middleware\IdentifyTenant;
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;
return Application::configure(basePath: dirname(__DIR__))
->withRouting(
web: __DIR__ . '/../routes/web.php',
api: __DIR__ . '/../routes/api.php',
commands: __DIR__ . '/../routes/console.php',
health: '/up',
// then: function () {
// Route::middleware(['api', 'identify.tenant'])
// ->prefix('api')
// ->name('api.')
// ->group(base_path('routes/api.php'));
// }
)
->withMiddleware(function (Middleware $middleware) {
$middleware->trustProxies(at: '*');
$middleware->append(IdentifyTenant::class);
// $middleware->prependToGroup('api', IdentifyTenant::class);
$middleware->api(prepend:[
IdentifyTenant::class,
]);
$middleware->alias([
'identify.tenant' => IdentifyTenant::class,
]);
$middleware->web(append: [
\App\Http\Middleware\HandleInertiaRequests::class,
\Illuminate\Http\Middleware\AddLinkHeadersForPreloadedAssets::class,
]);
//
})
->withExceptions(function (Exceptions $exceptions) {
//
})->create();
I tried to apply ItendifyTenant class to api.php files. but could not succeed. If I put this $middleware->append(IdentifyTenant::class); it applied to all routes files. If I $middleware->prependToGroup('api', IdentifyTenant::class); it does not work. In laravel, 8, 9, 10. It was quite easy. Assist is appreciated.
Please or to participate in this conversation.