Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon Laravel 11 route service provider


Laravel 11 route service provider


In Laravel 11, the `RouteServiceProvider` has been removed, and the routing configuration is now handled differently. Here's how you can configure routes in Laravel 11:

Configuring Routes in `bootstrap/app.php`

Instead of using the `RouteServiceProvider`, you can configure your routes directly in the `bootstrap/app.php` file:

php
return Application::configure(basePath: dirname(__DIR__))
    ->withRouting(
        web: __DIR__.'/../routes/web.php',
        api: __DIR__.'/../routes/api.php',
        commands: __DIR__.'/../routes/console.php',
        health: '/up',
        apiPrefix: '/api',
    )
    // ...
    ->create();

In this example:
- `web` specifies the path to the web routes file
- `api` specifies the path to the API routes file
- `commands` specifies the path to the console routes file
- `health` specifies the health check route
- `apiPrefix` specifies the prefix for API routes

Configuring Middleware in `bootstrap/app.php`

Middleware configuration is also done in the `bootstrap/app.php` file:

php
return Application::configure(basePath: dirname(__DIR__))
    // ...
    ->withMiddleware(function (Middleware $middleware) {
        // Apply middleware to all routes
        $middleware->use([ExampleMiddleware::class]);

        // Use it only in the web routes
        $middleware->web([ExampleMiddleware::class]);

        // API only
        $middleware->api([ExampleMiddleware::class]);
    })
    // ...
    ->create();

Here, you can specify middleware to be applied to all routes, web routes, or API routes.

Scheduling Tasks in `routes/console.php`

Task scheduling is now done in the `routes/console.php` file:

php
use Illuminate\Support\Facades\Schedule;

Schedule::command('inspire')->hourly();
Schedule::job(ExampleJob::class)->daily();

You can use the `command` method to schedule a command or the `job` method to schedule a job.

In summary, the routing configuration in Laravel 11 is now handled in the `bootstrap/app.php` file, where you can specify routes, middleware, and scheduling tasks. The `RouteServiceProvider` has been removed, simplifying the routing setup process.

Citations:
[1] https://martinjoo.dev/laravel-11-routeserviceprovider
[2] https://martinjoo.dev/whats-new-in-laravel-11
[3] https://laracasts.com/discuss/channels/laravel/laravel-11-routeserviceprovider?reply=929412
[4] https://laracasts.com/discuss/channels/laravel/laravel-11-routeserviceprovider-1
[5] https://laravel.com/api/11.x/Illuminate/Foundation/Support/Providers/RouteServiceProvider.html