Laravel-10 Laravel-11 comparison, changes

Laravel-10 Laravel-11 comparison, changes

We know Laravel has gone through a lot of changes in Laravel 11. The directory has been very much streamlined as compared to previous versions. We will discuss in details.

Laravel Request LifeCyle

First lets come to request life cyle
Laravel 10 index.php file


Lets see in Laravel 11


We see the request Lifecycle is almost identical. The entry point of application is index.php Which loads all composer generated autoload files.

Then an instance of application is created in bootstrap/app.php. Which is service container
In Laravel 10

Here we know that request comes through different servers via HTTP Kernel or console kernel
Depending on type of requests.

The line above binds the IlluminateContractsHttpKernel interface to the AppHttpKernel class.


The line above binds the IlluminateContractsConsoleKernel interface to the AppHttpConsole class.

In Laravel, the ExceptionHandler class is responsible for handling exceptions that occur during the execution of your application.
This line tells Laravel’s service container to bind the IlluminateContractsDebugExceptionHandler interface to the AppExceptionsHandler class using the singleton method.
Here’s what’s happening:
IlluminateContractsDebugExceptionHandler is an interface provided by Laravel that defines methods for handling exceptions.
AppExceptionsHandler is a class within your Laravel application that implements the ExceptionHandler interface. It contains methods for handling various types of exceptions thrown by your application.
Singleton is a rule that says a class can only have one object, or instance.Laravel’s service container uses this Singleton rule.
This means that for a class like AppExceptionsHandler, there’s only ever one object of this class during the app’s lifetime. It’s like having a single manager for handling exceptions in the app. No matter where you are in the app, you’re always working with the same manager.
In Laravel 10 bootstrap/app.php
If we see instance of application

We see it inherits the Container class and implements HttpKernelInterFace.
The handle method in the HttpKernelInterface receives an HTTP Request and processes it to generate a Response


HttpKernelInterface is a key part of the Symfony framework, defining how HTTP requests should be handled and responses generated.
Laravel 11
If we see this
bootstrap/app.php

We see here bootstrap/app.php we see its a bit different an application instance is created.
Here its contains Middleware,with routing and withExceptions method.It aso extends the container and implements HTTPKernelInterface

We see HttpKernelInterface has same handle method

No app/Http/Kernel.php in Laravel 11
In Laravel 11 there is no Kernel.php
In Laravel 10 as we know all middlewares are there in HTTP kernel.php.The HTTP kernel extends the IlluminateFoundationHttpKernel class, which defines an array of bootstrappers.
In Laravel 10
In Kernel.php this is what it contains

If we see appHttpKernel.php

It inherits the HttpKernel
If we look at this directory

It contains an array of bootstrapers which contains list of tasks like error handling,configuration logging,Detecting environment of application etc.

This is also same in Laravel 11

So you can follow this Directory structure and see Kernel.php.
Registering middleware in Laravel 11
For registering middleware in Laravel 11 like previously shown its in bootstrap/app.php

Here you need to use withMiddleware function
No middleware directory. By default Laravel 11 has no directory.

To create a custom Middleware in Laravel11 **

Now we see a custom directory has been created.
**Lets say you would want to use a common middleware like auth in Laravel 11

We need to register in manually
Alias Middleware in Laravel 11

The Authenticate class comes from use IlluminateAuthMiddlewareAuthenticate;
Alias Middleware in Laravel 10
in Laravel 10 it used to come from appHttpKernel.php *which contains as variable named *$middlewareAliases which contains an alias for using all middlewares so they can be assigned to middleware and groups more conveniently

Priority Middleware in Laravel 10
In Laravel 10 we could set that in appHttpKernel.php.
protected $middlewarePriority;
But in Laravel 11 we need to do it with To arrange all middlewares in priority we need to use $prirority

Here the AdminRedirect is placed and has the namespace AppHttpMiddlewareAdminRedirect


We can see the custom middleware namespace here.
Service Providers
In Laravel 10
We know in Laravel ServiceProviders play a vital role in bootstrapping the application.
We may register service container bindings, event listeners, middleware, and even routes. Service providers are the central place to configure your application.
In config/app.php you can see the Providers array

In Laravel 11
But in Laravel 11 we see there is only one ServiceProvider AppServiceProvider.
They are registered in bootstrap/providers.php

The register function is used to connect things to the Service Container, which is a tool for managing class dependencies. It’s like a box where we can store and retrieve things we need later.
However, it’s important to only use register for binding things to the Service Container. We should not use it to set up event listeners, routes, or any other features. These setups should be done elsewhere, not in the register method.
What is boot method
It is called when a Laravel model is instantiated. It can do several things like listening to Event Listeners.
Service Provider Directory of Laravel 10

Service Provider Directory of Laravel 11

There is no RouteService Provider in Laravel 11 in config/bootstrap.php we use withRouting

Laravel Request Life Cycle in short
In Laravel, a user request is received by a route or controller, processed (like fetching data or preparing a page), a response is created, checked by middleware (like security checks), handled by the HTTP kernel (the core of Laravel), and finally sent back to the user.

Scheduling
In Laravel 10 we use use register all our schedule commands in appConsoleKernel.php

Here we see 2 schedule commands which are located in appConsoleKernel.php
So here when we see the schedule:list we can see all schedule commands.
In Laravel 11 there is no appConsoleKernel.php so so we need to schedule all commands in routesconsole.php

APi routes In Laravel 10 sanctum included as a package

If we want to create an APi in Laravel 10 its very easy if we look at routes directory we will see api.php inside routes folder.
If we check composer.json we will see

So we see there is already api.php. If we create a route in api.php

Inside routes/api.php

Laravel 11
But in Laravel 11 if we see routes directory we can see there is no api.php.

So for writing api routes you need to install api.
php artisan install:api
Here we see that after running the command a scaffolding is created already

So here we see Laravel instructing to Add LaravelSanctumHasApiTokens to User Model.This is to add API authentication.
User.php in Laravel10

We see here User Model does not have HasApiToken

So here we to add HasApiTokens we can do

If we go to bootstrap/app.php we can see

We see here that api routes have been registered.
To check the route list


AddMiddleware If we see api.php in Laravel 10

Middleware needed to be enabled for Laravel Apis frontend in Laravel 10

LaravelSanctumHttpMiddlewareEnsureFrontendRequestsAreStateful::class,

Middleware needed to be enabled for Laravel Apis in Laravel 11


No $casts property in Laravel 11.
In Laravel 10 We could do something like this

But in Laravel 11 if we see the User.php.Instead we need to use this

Sqlite out of the box

Unlike previous versions in a fresh installation of Laravel you get sqlite outof the box.
If you use global laravel installer you will get options.So you dont need to worry.

laravel new example-app

If you use this command composer create-project laravel/laravel example-app you will get sqlite out of the box.If you go in .env files


So its better to use laravel global installer instead of using composer where you will get to choose between type of database you want.

Leave a Reply

Your email address will not be published. Required fields are marked *