Study Guide 7 Middleware in Laravel Part 1
Study Guide 7 Middleware in Laravel Part 1
WEB SYSTEMS
AND
TECHNOLOGIES
2
Module 7:
Middleware in Laravel
Laravel Encryption and Hashing
04/15/2025 2
How Middleware Works
When a request hits your Laravel app, it passes through
a stack of middleware before reaching your controller or
route.
Each middleware class has a handle() method that can:
• Allow the request to continue ($next($request))
• Block the request
• Modify the request/response
Creating Middleware
php artisan make:middleware MiddlewareName
04/15/2025 3
Types of Middleware
1. Route Middleware
Applied to specific routes or controllers.
Alias in bootstrap/app.php:
$middleware->alias([
'auth' => \App\Http\Middleware\Authenticate::class,
]);
Use in routes:
Route::get('/dashboard', function () {
// ...
})->middleware('auth');
04/15/2025 4
Example:
Create a route middleware: DownForMaintenanceMW
It will be created in App/Http/Middleware
Inside the function handle write the codes:
if(env("APP_ENV")=="local"){
return redirect("/maintenance");
}
In web.php, create a route:
Route::get('/maintenance',[PagesController::class,"maintenance"]);
In PagesController, write:
function maintenance() {
return "Application is down for maintenance";
}
04/15/2025 5
Example:
Go to bootstrap/app.php in the middleware function and write:
$middleware->alias(['maintenance' =>
\App\Http\Middleware\
DownForMaintenanceMW::class,
]);
Go to web.php and write the following code in any route before the “;”:
->Middleware('maintenance’)
If you want to add more middleware use the code:
->Middleware(['maintenance’,’anothermiddleware’])
04/15/2025 6
Types of Middleware
2. Global Middleware
Runs on every request, regardless of route.
Registered in bootstrap/app.php (Laravel 12+) using:
$middleware->append([
\App\Http\Middleware\CheckForMaintenanceMode::class,
]);
Use append() adds your middleware to the end of the global stack.
Use prepend() if you want it to run before Laravel’s core middleware.
04/15/2025 7
Example:
Create a global middleware: GlobalMiddleWare
Inside the function handle write the codes:
echo "This is from our global middleware";
Go to bootstrap/app.php in the middleware function and write:
$middleware->append([
\App\Http\Middleware\GlobalMiddleWare ::class,
]);
04/15/2025 8
Types of Middleware
3. Middleware Groups
A set of middleware bundled under a group name.
Define in bootstrap/app.php:
$middleware->group('web', [
\App\Http\Middleware\EncryptCookies::class,
\App\Http\Middleware\StartSession::class,
]);
Use in routes:
Route::middleware('web')->group(function () {
// web routes here
});
04/15/2025 9
Example:
Create Your Middleware Classes:
php artisan make:middleware ExampleMiddlewareOne
php artisan make:middleware ExampleMiddlewareTwo
Open bootstrap/app.php and find the ->withMiddleware() method.
Then define your group like this:
$middleware->group('custom-group', [
\App\Http\Middleware\ExampleMiddlewareOne::class,
\App\Http\Middleware\ExampleMiddlewareTwo::class,
]);
Now that you've created a group named custom-group, you can apply it in
your route files like so:
Route::middleware('custom-group')->group(function () {
Route::get('/test', function () {
return 'Middleware group works!';
});
});
04/15/2025 10
Laravel Hashing
Hashing is a one-way process. It transforms data (like a password) into a
fixed-length string that cannot be reversed or decrypted. Once hashed, the
original value can never be retrieved.
Laravel uses secure hashing algorithms like bcrypt, argon2,
and argon2id via the Hash facade.
Use Hashing For:
• Passwords
• Authentication tokens
• Any data that should never be reversed
How to Hash in Laravel
use Illuminate\Support\Facades\Hash;
// Storing a password
$user->password = Hash::make($request->password);
// Verifying it later
if (Hash::check($request->password, $user->password)) {
// Password is correct
04/15/2025 11
}