In laravel 11 and 12, making new routes files and applying middleware and domain to them difficult or impossible? #55249
-
Laravel Version11.43 and 12.1 PHP Version8.3 Database Driver & VersionPGSQL DescriptionIn 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, However, I am also senior developer working on Laravel for 4 years and php for 7 years. I am trying to add middleware to just api or just api.php 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. Are you guys making laravel framework harder for such small adjustment or adding such bit thing, I could not understand.
I tried to apply ItendifyTenant class to api.php files. but could not succeed. Steps To ReproducePlease make as simple or easy as like in Laravel 8, 9 or 10.
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
Maybe this can help you |
Beta Was this translation helpful? Give feedback.
-
@bhojkamal you can have full control on route registration by passing a <?php
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;
use Illuminate\Routing\Router;
return Application::configure(basePath: \dirname(__DIR__))
->withRouting(function (Application $app, Router $router) {
$router->middleware('api')
->prefix('/api')
->as('api.')
->group($app->basePath('routes/api.php'));
$router->middleware('web')
->group($app->basePath('routes/web.php'));
})
// in case you want to keep routes/console.php
// app/Console/Commands is already registered nonetheless
->withCommands([\dirname(__DIR__) . '/routes/console.php'])
->withMiddleware(function (Middleware $middleware) {
// ... your custom Middleware config
})
->withExceptions(function (Exceptions $exceptions) {
// ...
})
->create(); See the last part of this section on the docs: https://laravel.com/docs/12.x/routing#routing-customization |
Beta Was this translation helpful? Give feedback.
-
Related discussion: "Reproduction" repo: https://github.com/rodrigopedra/discussions-55182 Note: The code in reproduction repo works using standard Laravel 11/12 code. The repo actually serves as a base, so OP can further explain the issue, as I could not reproduce it. |
Beta Was this translation helpful? Give feedback.
@bhojkamal you can have full control on route registration by passing a
\Closure
to thewithRouting
method: