What Is The Use Of Creating Middleware In Laravel Framework

admin_img Posted By Bajarangi soft , Posted On 17-09-2020

Middleware provide a convenient mechanism for filtering HTTP requests entering your application.

What Is The Use Of Creating Middleware In Laravel Framework

Defining Middleware

To create a new middleware, use the make:middleware Artisan command:

Example(1)
Run below command in command prompt to create middleware

php artisan make:middleware userdemoMiddleware


Open app\console\kernel.php file and create below command in it

protected $routeMiddleware = [
    'user_data' => \App\Http\Middleware\userdemoMiddleware::class,
];


This command will place a new userdemoMiddleware class within your app/Http/Middleware directory. In this middleware, we will only allow access to the route if the supplied login user_id is equal to one. Otherwise, we will redirect the users back to the home URI:
 

<?php

namespace App\Http\Middleware;

use Closure;
use DB;
use Auth;

class userdemoMiddleware
{
    /**

     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $isAdmin = Auth::user()->id;
        if($isAdmin == 1){
            return $next($request);
        }
        return redirect('/');
    }
}


As you can see, if the given login  user_id is  equal to 1, the middleware will return an HTTP redirect to the client; otherwise, the request will be passed further into the application. To pass the request deeper into the application (allowing the middleware to "pass"), call the $next callback with the $request.

It's best to envision middleware as a series of "layers" HTTP requests must pass through before they hit your application. Each layer can examine the request and even reject it entirely.

All middleware are resolved via the service container, so you may type-hint any dependencies you need within a middleware's constructor.



After creating middleware create route in web.php to middleware applied

Route::middleware(['auth','user_data'])->group(function() {
    Route::get('demo', 'demoController@index');
});


When you call demo method it will go to demoController index function

1.So create democontroller and implement code as below 

php artisan make:controller demoController

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class demoController extends Controller
{
    public function index(){

        return 'Hello World,Welcome to Bajarangisoft';
    }
}


just pass below url in google chrome

http://localhost/laraveldemoproject/public/demo



Before & After Middleware

Whether a middleware runs before or after a request depends on the middleware itself. For example, the following middleware would perform some task before the request is handled by the application:
 

<?php

namespace App\Http\Middleware;

use Closure;

class BeforeMiddleware
{
    public function handle($request, Closure $next)
    {
        // Perform action

        return $next($request);
    }
}



However, this middleware would perform its task after the request is handled by the application:

<?php

namespace App\Http\Middleware;

use Closure;

class AfterMiddleware
{
    public function handle($request, Closure $next)
    {
        $response = $next($request);

        // Perform action

        return $response;
    }
}

 

Related Post