Laravel-10 Laravel-11 comparison, changes
Tags: laravel, php, backend, webdev
Published: 2024-04-25 22:01:32

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 

![RequestLifeCycleLaravel10](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h30wspvyxr5udjwncjuq.png)
Lets see in Laravel 11

![RequestLifeCycleLaravel11](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m8cxpg1mldgysd4jk9nn.png)
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**

![bootstrap/app.phpLaravel10](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5qpd47gm97wm7ch8ayu4.png)

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

![HttpRequest](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tfykgiuu7nk6jgzpffcu.png)

The line above binds the **Illuminate\Contracts\Http\Kernel** interface to the **App\Http\Kernel** class.

![ConsoleRequest](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o23ot4us5d87q6q1z6ty.png)
 The line above binds the **Illuminate\Contracts\Console\Kernel** interface to the **App\Http\Console** class.

![ErrorHandling](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3dgdgmii8yfszyx4ra4h.png)

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 **Illuminate\Contracts\Debug\ExceptionHandler** interface to the **App\Exceptions\Handler** class using the singleton method.
Here's what's happening:
**Illuminate\Contracts\Debug\ExceptionHandler** is an interface provided by Laravel that defines methods for handling exceptions.
**App\Exceptions\Handler** 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 App\Exceptions\Handler, 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 
![ErrorHandling](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/byfzp3l37yynjti6k902.png)
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

![handlemethod](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wplrwrgra70nrhg4cx0d.png)
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
![Larvael11/bootstrap/app.php](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cbb393960f8ztou2pigp.png)
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
![AppInstanceLaravel11](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x1rsgyn4wwiu0974az67.png)
We see HttpKernelInterface has same handle method
![handlemethod](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r7edk1ehxbfm122yrv6z.png)
**Middleware**

In Laravel 10 as we know all middlewares are there in **app\Http\Kernel.php**.

The HTTP kernel extends the **Illuminate\Foundation\Http\Kernel** class, which defines an array of bootstrappers.
In Laravel 10
In **Kernel.php** this is what it contains 
![middleware](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hrduklhwlicz7tea87r7.png)
If we see app\Http\Kernel.php 
![InheritingHttpKernel](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kl852gsls6rscotlix4z.png)
It inherits the HttpKernel 
If we look at this directory 
![HttpKernel](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nbqme9064nplnt10sea3.png)
It contains an array of bootstrapers which contains list of tasks like error handling,configuration logging,Detecting environment of application etc.
![Bootstrappers](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c0kwq61q9l77q4hj9y2x.png)
This is also same in Laravel 11
![DirectoryStructureofKernel.php](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l8s3es2bfj4q1cbeu9t2.png)
So you can follow this Directory structure and see Kernel.php.
**Registering middleware in Laravel 11**
We know there are three types of middleware in Laravel. If we look at **app\Http\Kernel.php** in **Laravel 10** we can see 
**Global Http middleware**
![Global Http middlewareLaravel10](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cc53tftmmrbhfoi2wiu8.png)
As we see in the image above in Global Http middleware these middlewares are run during every request to your application.

**Route Middleware**
![RouteMiddlewareLaravel11](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hgn4k9le5pn4jerlhan2.png)
The application's route middleware groups.These middlewares are applied to either web or api.

**Alias Middleware**
![AliasMiddlewareLaravel10](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u6mkl0tpy74vvr3lwucu.png)

Aliases may be used to conveniently assign middleware to routes and groups. You can assign these middlewares as per your need.

In **Laravel 11** there is no **app\Http\Kernel.php **
**So where are these Middlewares?** 
![NamespacewithMiddleWare](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ztvtc4sgy2m2rk6uzsbo.png)
In bootstrap/app.php if we point  the mouse withMiddleware function we can see the namespace from where its coming.

This is the directory
![DirectoryofwithMiddleware](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u70bagjix2bkurzbq6xy.png)
 **withMiddlewareFunction**
![withMiddlewareFunction](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jdqwre55nnrtcws4iw3l.png)
We see it has all methods of middlewares.
If we point out the mouse again we can see the namespace
![MiddlewareNamespace](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a05ponewa1agm8rzdobp.png)
The **directory** is as follows 
![Directory](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/83atwyp2gimd47zr4uxh.png)
If we go to the directory, we can see
**Global Middleware**
![Global Middleware function](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o732f2igvu29dz4mzntv.png)
** Middleware Groups**
![MiddlewareGroups](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gl9t04i2kqjdfwm6a1y8.png)
***Alias Functions***
![Alias Functions](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/82vmx25x945ohffc345e.png)

So as we see we can find all middlewares but they have been moved inside vendor. For using them we need to manually register unlike when they where in **app\Http\Kernel.php** in Laravel10.
For registering middleware in Laravel 11 like previously shown its in bootstrap/app.php 
![MiddlewareLaravel11](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9hu5eu53soxzx19q9dzl.png)
Here you need to use withMiddleware function
**No middleware directory. By default Laravel 11 has no directory.**



![appHttpDirectory](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3u8j6vab19kt6mxdwtlq.png)

**To create a custom Middleware in Laravel11 ** 
![CustomMiddleware](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wg4tkzcfjdi914qngf98.png)
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**
![Registeringcommonmiddleware](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6bhb1pgtsxsdwgw3qnaw.png)
 
But in Laravel 11 we need to do it with To arrange all middlewares in priority we need to use $prirority
![Laravel11](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h3albledo7pto92syajc.png)
Here the AdminRedirect is placed and has the namespace **App\Http\Middleware\AdminRedirect**

![CustomMiddleware namespace](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vh9uf15l3esvbov12obm.png)
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 

![providers](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yjnw6efr5ytuep1qm7k7.png)

**In Laravel 11**
But in Laravel 11 we see there is only one ServiceProvider AppServiceProvider.
They are registered in bootstrap/providers.php
![providers in Laravel11](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ef0busmgadcxyyze03bk.png)
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**
![ServiceProviderLaravel10](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z1ea8e2y0o3j7yuqrmap.png)
**Service Provider Directory of Laravel 11**
![ServiceProvider](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6p4b46f8dtj2qqzc0212.png)
There is no **RouteService Provider** in Laravel 11 in config/bootstrap.php we use withRouting 
![withRouting](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/od1wsvakzih8tod5u1bs.png)

**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 **app\Console\Kernel.php**

![In app\Http\Kernel.php](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wvt224buhz3u3hqn2315.png)

Here we see 2 schedule commands which are located in app\Console\Kernel.php 
So here when we see the schedule:list we can see all schedule commands. 
In Laravel 11 there is no app\Console\Kernel.php so so we need to schedule all commands in routes\console.php 

![console](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y0smbqe6reizokyxuqfm.png)

**APi routes In Laravel 10 sanctum included as a package** 

![Package.jsonatLaravel10](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lbb39fc4oixzgnpm4uen.png)
In Laravel 11 its not included by default
If we see composer.json after a brand new installation.
![Laravel11](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b4mt8pr7xx5k6lvz6i43.png)

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

![Apiroutes](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ttford9y6jpir7r9xfqh.png)

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

Inside **routes/api.php**
![Routes](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ep8sjqsmhwnubso07zf5.png)

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

![Api](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0l6bioryyo39g8y2jand.png)

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

![ScaffoldingApi](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9tg5b2odesviizjfn0ec.png)

![ScaffoldingApi2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vjt98x2dpjr28rktfe6n.png)

So here we see Laravel instructing to Add Laravel\Sanctum\HasApiTokens to User Model.This is to add API authentication.
**User.php** in Laravel10

![UserModelLaravel11](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wx68kdjr25nsd4kebzic.png)

We see here User Model does not have **HasApiToken**

![UserModelHasApiToken](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dg4y8pxv6yveu2hnnrvr.png)

So here we to add HasApiTokens we can do 

![HasApiTokens](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/20bpg5633yqk28s44zfk.png)

If we go to **bootstrap/app.php** we can see
![Apiroutesinbootstrapapp.php](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bqs2r52vxjs325m3g5r2.png)

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

![RouteList](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/quwmw7abyxcqlptti3ls.png)
AddMiddleware If we see api.php in Laravel 10


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

![Laravel10FrontendApiMiddleware](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nzthhz2a42qg5wtfb3u4.png)

`\Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,`


**Middleware needed to be enabled for Laravel Apis in Laravel 11**
If we see the api group middleware in Laravel 11 we can see

![statefulApi](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q5dxoix4ovtuskcw3n3p.png)


![StatefulApi](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7fijkthqswk398yu7l3e.png)
  `$this->statefulApi ? \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class : null,`
The code checks if the statefulApi property is true.
If statefulApi is true, it applies the EnsureFrontendRequestsAreStateful middleware from Laravel Sanctum to maintain session data for API requests.
If statefulApi is not true, it doesn’t apply any middleware (returns null).
![ApiMiddlewareLaravel11](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nr8cx0qtisebqlzroiww.png)
No **$casts** property in Laravel 11. 
In Laravel 10 We could do something like this 

![Casts in Laravel10](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bwehobzuc4sa238t76jb.png)

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

![Casts function in Laravel10](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s1xt5ul6qptp4wlswur9.png)


**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`
![Optionsforlaravelglobalinstaller](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eb7chew7izn221stcyyc.png)

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

![Sqliteoutofboxforinstallingviacomposer](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m4jdlw9c6eda95zp4pl4.png)
So its better to use laravel global installer instead of using composer where you will get to choose between type of database you want.