In Laravel 10, we had a RouteServiceProvider class that handled loading up all your different routes. This is a snippet from that file in an older project I worked on using Laravel 10:

RouteServiceProvider.php

/** * Define your route model bindings, pattern filters, and other route configuration. */ public function boot(): void { $this->configureRateLimiting(); $this->routes(function () { $this->registerCentralDomainRoutes(); config('app.env') == 'local' && Route::middleware('web')->group(base_path('routes/test.php')); }); } 

You had access to almost all of the tools and features of the Laravel framework in the ServiceProvider because the core framework services had already been booted up. But now, in Laravel 12, the RouteServiceProcivider file no longer exists and we now register routes in the bootstrap/app.php file.

bootstrap/app.php

return Application::configure(basePath: dirname(__DIR__)) ->withRouting( web: [ base_path('routes/web/routes.php'), base_path('routes/web/auth.php'), base_path('routes/web/agent.php'), ...(config('app.env') === 'local' ? [base_path('routes/web/test.php')] : [], ], api: base_path('routes/api.php'), commands: base_path('routes/console/routes.php'), health: '/up', ) 

This new system doesn't allow me to access values from the .env file, so I can't do things like conditionally registering routes like before.

How can I do this in Laravel 12?

5 Replies 5

also, how can I change the question type from "Advice" back to a general question? I tried editing, but there doesn't seem to be an option to do that.

Likely need to delete and re-ask the question. This "Advice" thing is new (to me anyway), and I don't see an option to change the type either... There's a dropdown when creating a question, but nothing when editing 🤔

The things the comment above your old boot function say go in there (model bindings, pattern filters) seem to go into the boot function in the App\Providers\AppServiceProvider class in Laravel 12 (see https://laravel.com/docs/12.x/routing#parameters-global-constraints and https://laravel.com/docs/12.x/routing#explicit-binding ). Maybe you can access the .env values from there?

Is your question "why is it like this", or is your question "How can I achieve X"? Your title says one thing (for which the "advice" category you chose is the correct one) but the body of the post then asks something different. Perhaps you actually need two different posts - a new one to solve your specific problem, and this one to have a discussion about the design of the feature in Laravel?

Laravel provides two options depending on how complicated your logic is.

First, you can pass a callback to the then parameter to perform extra routing logic on top of your existing logic.

use Illuminate\Support\Facades\Route; return Application::configure(basePath: dirname(__DIR__)) ->withRouting( web: [ base_path('routes/web/routes.php'), base_path('routes/web/auth.php'), base_path('routes/web/agent.php'), ], api: base_path('routes/api.php'), commands: base_path('routes/console/routes.php'), health: '/up', then: function ($app) { if ($app->isLocal()) { Route::middleware('web')->group(base_path('routes/web/test.php')); } } ) 

The other option is to take over the routing logic yourself by passing a callback to the using parameter. When this is done, the web, api, pages, health, apiPrefix, and then parameters are ignored, since you're telling Laravel you're handling the logic yourself. The commands and channels parameters will still be handled properly.

use Illuminate\Support\Facades\Route; return Application::configure(basePath: dirname(_DIR_)) ->withRouting( commands: base_path('routes/console/routes.php'), using: function () { // Handle the web, api, pages, and health routes yourself here } ) 

Laravel docs for this info: https://laravel.com/docs/12.x/routing#routing-customization

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.