Tuesday, 09 Apr, 2024 -144

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: 1958
img
Xampp Tutorial Create Your Own Local Test Server
02 Apr, 2024 view: 885
img
How to Install Laravel
02 Apr, 2024 view: 819
img
PHP is not recognized as an internal or external command
02 Apr, 2024 view: 610
img
How to Install Composer on Windows
01 Apr, 2024 view: 608
img
Summernote image upload
01 Nov, 2023 view: 553
img
How to clear select2 selected value in js
01 Nov, 2023 view: 426
img
Zipping and Unzipping Files in Linux
02 Apr, 2024 view: 376
img
How to add Datepicker in Bootstrap
26 Apr, 2024 view: 372
img
How to Import a MySQL Database using Command Line
02 Apr, 2024 view: 366
img
How to Installation Laragon
01 Apr, 2024 view: 213
img
How to Installing an SSL certificate on your server using cPanel
06 Apr, 2024 view: 183
img
Paypal Payment Gateway Integration With Laravel
06 Apr, 2024 view: 153
img
How to Use Laravel Middleware to Protect Your Application
09 Apr, 2024 view: 144
img
Laravel Cache Clear Command Php Artisan Optimize
09 Apr, 2024 view: 144
img
why important website development learn
10 Apr, 2024 view: 136
img
How to Integrate Stripe Payment in Laravel
06 Apr, 2024 view: 134
img
Laravel Authentication Tutorial
09 Apr, 2024 view: 133
img
laravel qr code generator package
18 Apr, 2024 view: 132
Trending