Tuesday, 09 Apr, 2024 -211

How to Use Laravel Middleware to Protect Your Application

Laravel  Middleware

Between a request and a response, middleware acts as a link. It is a type of filtering mechanism. This lesson explains  the middleware mechanism in Laravel.

Laravel verifies  user and application authentication if the user is authenticated redirect  successfully  otherwise if not authenticate then not redirect.


*Middleware Create Command :


php artisan make:middleware <middleware-name>

<middleware-name> Here write you middleware name 

After creating middleware you will find it at  app/Http/Middleware directory.

Example :


Following the example and understand the tutorial middleware mechanism

Step 1 : We create admin middleware , let us know our middleware create command 

Php artisan make:middleware adminMiddleware.

Step 2 :after create middleware successfully , you will receive following output 

 

Step 3 : AdminMiddleware will be created at app/Http/Middleware. The newly created file will have the following code already created for you.

<?php


namespace App\Http\Middleware;

use Closure;


class AdminMiddleware {

   public function handle($request, Closure $next) {

      return $next($request);

   }

}


Middleware Registering

  The following is the code for app/Http/Kernel.php

<?php


namespace App\Http;

use Illuminate\Foundation\Http\Kernel as HttpKernel;


class Kernel extends HttpKernel {

   protected $middleware = [

      \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,

      \App\Http\Middleware\EncryptCookies::class,

      \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,

      \Illuminate\Session\Middleware\StartSession::class,

      \Illuminate\View\Middleware\ShareErrorsFromSession::class,

      \App\Http\Middleware\VerifyCsrfToken::class,

   ];

  

   protected $routeMiddleware = [

      'auth' => \App\Http\Middleware\Authenticate::class,

      'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,

      'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,

      Admin=> \App\Http\Middleware\AdminMiddleware::class,

   ];

}


Step 4: Declare middleware 

Route::get('/', function () {

    //

})->middleware('web');

 

Route::middleware(['web'])->group(function () {

    //

});


Tags
Most Popular
img
How to install WordPress
02 Apr, 2024 view: 2083
img
Xampp Tutorial Create Your Own Local Test Server
02 Apr, 2024 view: 1183
img
How to Install Laravel
02 Apr, 2024 view: 942
img
PHP is not recognized as an internal or external command
02 Apr, 2024 view: 723
img
How to Install Composer on Windows
01 Apr, 2024 view: 723
img
Summernote image upload
01 Nov, 2023 view: 668
img
How to clear select2 selected value in js
01 Nov, 2023 view: 494
img
Zipping and Unzipping Files in Linux
02 Apr, 2024 view: 441
img
How to add Datepicker in Bootstrap
26 Apr, 2024 view: 439
img
How to Import a MySQL Database using Command Line
02 Apr, 2024 view: 430
img
How to Installation Laragon
01 Apr, 2024 view: 283
img
How to Installing an SSL certificate on your server using cPanel
06 Apr, 2024 view: 255
img
Paypal Payment Gateway Integration With Laravel
06 Apr, 2024 view: 216
img
How to Use Laravel Middleware to Protect Your Application
09 Apr, 2024 view: 211
img
Laravel Authentication Tutorial
09 Apr, 2024 view: 207
img
Laravel Cache Clear Command Php Artisan Optimize
09 Apr, 2024 view: 207
img
laravel qr code generator package
18 Apr, 2024 view: 198
img
How to Integrate Stripe Payment in Laravel
06 Apr, 2024 view: 197
img
why important website development learn
10 Apr, 2024 view: 194
img
How to Install Laravel Valet on Windows
26 Feb, 2025 view: 21
img
Learn how to implement Single Sign-On (SSO) in Node.js using Passport.js and TypeScript for enhanced user experience and secure ...
26 Feb, 2025 view: 6
Trending