Middleware can verify the user's authentication status and ensure they have the necessary credentials to access certain routes or resources.
Basically, we use three types of Middleware.
1. Global Middleware
2. Route Middleware
3. Group Middleware
Example of Global Middleware
Suppose you need to restrict some users whose ages are less than 18th
First, create a middleware name as UserresMidddleware
php artisan make:middleware UserresMidddleware
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
class UserresMidddleware
{
public function handle(Request $request, Closure $next)
{
return $next($request);
}
}
Create a view page name as noaccess.blade.php . I am simple write <h1> No access </h1>
Now open your middleware page and give an if statement.
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
class UserresMidddleware
{
public function handle(Request $request, Closure $next)
{
if($request->age && $request->age<18)
{
return redirect('noaccess');
}
return $next($request);
}
}
After that go to app karnel page
Call your middleware class here
/**
* The application's global HTTP middleware stack.
*
* These middleware are run during every request to your application.
*
* @var array<int, class-string|string>
*/
protected $middleware = [
\App\Http\Middleware\UserresMidddleware::class,
\App\Http\Middleware\TrustProxies::class,
\Fruitcake\Cors\HandleCors::class,
\App\Http\Middleware\PreventRequestsDuringMaintenance::class,
\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
\App\Http\Middleware\TrimStrings::class,
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
];
If , sending a request ?age=15
Create New middleware for the route. php artisan make:middleware MyrouteMiddleware ..
Create a New Middleware php artisan make:middleware NameOfMiddleware This is..
Middleware can verify the user's authentication status and ensure they have the necessary credential..
We have created a view page name as login.blade.php <div class="col-sm-4"><h3>L..
Suppose you have data and you need to check it by clicking or page onload. first, create a controll..
Blade is a templating engine used in Laravel, a popular PHP framework. It provides a simple and expr..
Current Url <div class="container-fluid p-5 bg-primary text-white text-center"><h1..
A component is a group of code like a header or footer. Which are re-useable. Common part ofapplicat..
In Laravel, a controller is a class that handles the logic and acts as an intermediary between themo..
It's Easy to create routes. Already we know That all route files should be kept in the routes ..
The blade is the templating engine used in Laravel, which is a popular PHP framework. It provides at..
Laravel is a popular open-source PHP framework known for its elegant syntax, expressive features, an..
In Laravel, you can set dynamic page titles by utilizing the power of Blade templating and passing d..
To set up a Laravel project with Bootstrap, you can follow these steps: Create a project, I..
How to Install Laravel using Composer First, we need to install Composer. check How to inst..
How to Install Composer on Linux Open terminal php -r "copy('https://getcomposer.o..
Get the latest news and updates by signing up to our daily newsletter.We won't sell your email or spam you !