DEV Community

Tanzim Ibthesam
Tanzim Ibthesam

Posted on

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

RequestLifeCycleLaravel10
Lets see in Laravel 11

RequestLifeCycleLaravel11
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

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

HttpRequest

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

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

ErrorHandling

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
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
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
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
We see HttpKernelInterface has same handle method
handlemethod
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
If we see app\Http\Kernel.php
InheritingHttpKernel
It inherits the HttpKernel
If we look at this directory
HttpKernel
It contains an array of bootstrapers which contains list of tasks like error handling,configuration logging,Detecting environment of application etc.
Bootstrappers
This is also same in Laravel 11
DirectoryStructureofKernel.php
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
As we see in the image above in Global Http middleware these middlewares are run during every request to your application.

Route Middleware
RouteMiddlewareLaravel11
The application's route middleware groups.These middlewares are applied to either web or api.

Alias Middleware
AliasMiddlewareLaravel10

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
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
withMiddlewareFunction
withMiddlewareFunction
We see it has all methods of middlewares.
If we point out the mouse again we can see the namespace
MiddlewareNamespace
The directory is as follows
Directory
If we go to the directory, we can see
Global Middleware
Global Middleware function
** Middleware Groups**
MiddlewareGroups
Alias Functions
Alias Functions

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
Here you need to use withMiddleware function
No middleware directory. By default Laravel 11 has no directory.

appHttpDirectory

To create a custom Middleware in Laravel11 **
CustomMiddleware
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

But in Laravel 11 we need to do it with To arrange all middlewares in priority we need to use $prirority
Laravel11
Here the AdminRedirect is placed and has the namespace App\Http\Middleware\AdminRedirect

CustomMiddleware namespace
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

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
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
Service Provider Directory of Laravel 11
ServiceProvider
There is no RouteService Provider in Laravel 11 in config/bootstrap.php we use withRouting
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 app\Console\Kernel.php

In app\Http\Kernel.php

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

APi routes In Laravel 10 sanctum included as a package

Package.jsonatLaravel10
In Laravel 11 its not included by default
If we see composer.json after a brand new installation.
Laravel11

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

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

Inside routes/api.php
Routes

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

Api

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

ScaffoldingApi2

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

We see here User Model does not have HasApiToken

UserModelHasApiToken

So here we to add HasApiTokens we can do

HasApiTokens

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

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

RouteList
AddMiddleware If we see api.php in Laravel 10

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

Laravel10FrontendApiMiddleware

\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

StatefulApi
$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
No $casts property in Laravel 11.
In Laravel 10 We could do something like this

Casts in Laravel10

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

Casts function in Laravel10

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

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
So its better to use laravel global installer instead of using composer where you will get to choose between type of database you want.

Top comments (2)

Collapse
 
joydeep-bhowmik profile image
Joydeep Bhowmik • Edited

Not many laravel enthusiast here! Huh?
Good post btw

Collapse
 
tanzimibthesam profile image
Tanzim Ibthesam • Edited

This topic delves into architecture and inner workings, resulting in fewer people engaging compared to when I wrote about API authentication and CRUD, which garnered more views