If you’ve used Laravel 10 before, you’ll notice some changes in how you add special tools and rules. But don't worry, we’re here to show you how easy it is to use the new version.

When things change, it can slow you down. You might feel like you need extra time to learn the new way of doing things. But learning these changes will actually make your developing work easier and faster in the long run. Let's see what these changes are and how you can handle them like a pro.

In Laravel 11, there are two main changes we need to look at: adding providers and adding middleware aliases. We’ll compare how it was done in Laravel 10 and how it’s done now in Laravel 11.

If you’re already feeling a bit confused with new aliases, facades, missing kernel.php, and similar changes, don’t worry! We’ll guide you through every step. By the end of this guide, you’ll see how these changes make adding tools and rules to your Laravel projects simpler and more organized.

Solution with Code Examples

In Laravel 11, there are two main changes we need to look at: adding providers and adding middleware aliases. We’ll compare how it was done in Laravel 10 and how it’s done now in Laravel 11.

Adding Providers in Laravel 11

Providers are like extra helpers that add new features to your project. In Laravel 10, you added providers in the config/app.php file. In Laravel 11, you add them in the bootstrap/providers.php file.

Laravel 10:

                    // config/app.php
'providers' => [
    App\Providers\AppServiceProvider::class,
    App\Providers\AuthServiceProvider::class,
    // Other providers...
];

                  

Laravel 11

                    // bootstrap/providers.php
<?php

return [
    App\Providers\AppServiceProvider::class,
    App\Providers\FortifyServiceProvider::class,
    App\Providers\JetstreamServiceProvider::class,
];

                  

To add a custom provider in Laravel 11, just add it to the list:

                    // bootstrap/providers.php
<?php

return [
    App\Providers\AppServiceProvider::class,
    App\Providers\FortifyServiceProvider::class,
    App\Providers\JetstreamServiceProvider::class,
    // Custom added provider
    Artesaos\SEOTools\Providers\SEOToolsServiceProvider::class,
];

                  

Adding Middleware Aliases

Middleware controls how data flows in your application. In Laravel 10, middleware aliases were added in the app/Http/Kernel.php file. In Laravel 11, they are added in the bootstrap/app.php file.

Laravel 10:

                    // app/Http/Kernel.php
protected $routeMiddleware = [
    'auth' => \App\Http\Middleware\Authenticate::class,
    // Other middleware...
];
                  

Laravel 11

                    // bootstrap/app.php
<?php

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',
    )
    ->withMiddleware(function (Middleware $middleware) {
        // Add your middleware aliases here
        $middleware->alias([
            'SEOMeta'       => Artesaos\SEOTools\Facades\SEOMeta::class,
            'OpenGraph'     => Artesaos\SEOTools\Facades\OpenGraph::class,
            'Twitter'       => Artesaos\SEOTools\Facades\TwitterCard::class,
            'JsonLd'        => Artesaos\SEOTools\Facades\JsonLd::class,
            'JsonLdMulti'   => Artesaos\SEOTools\Facades\JsonLdMulti::class,
            'SEO'           => Artesaos\SEOTools\Facades\SEOTools::class,
            'localize'      => \Mcamara\LaravelLocalization\Middleware\LaravelLocalizationRoutes::class,
            'localizationRedirect' => \Mcamara\LaravelLocalization\Middleware\LaravelLocalizationRedirectFilter::class,
            'localeSessionRedirect' => \Mcamara\LaravelLocalization\Middleware\LocaleSessionRedirect::class,
            'localeCookieRedirect'  => \Mcamara\LaravelLocalization\Middleware\LocaleCookieRedirect::class,
            'localeViewPath'        => \Mcamara\LaravelLocalization\Middleware\LaravelLocalizationViewPath::class,
            'roleProtected' => \App\Http\Middleware\RoleMiddleware::class,
            'globalData' => \App\Http\Middleware\GlobalData::class,
        ]);
    })
    ->withExceptions(function (Exceptions $exceptions) {
        //
    })->create();

                  

And there you go! Laravel 11 may look different at first, but it makes customizing your web applications even easier. Keep practicing and happy coding!

Of course, you can always check the official documentation of Laravel 11, or check out other cool articles about Laravel here on floyk.com