Tuesday 21 March 2017

Topic 13 - Middleware

What is Middleware?

Basically middleware is a mechanism for filthering HTTP request entering your application. Middleware can be such allowing authenthicated users to redirect to a certain page without re-entering username and password onto login page again. Middleware are located in the app/Http/Middleware directory.

To create new middleware, we can use make:middleware Artisan command.

php artisan make:middleware CheckMaleGender

This will create new middleware file at app/Http/Middleware directory. The middleware we just create is to let male user can enter the application or otherwise redirect them back to home page.

<?php

namespace App\Http\Middleware;

use Closure;

class CheckMaleGender
{
    public function handle($request, Closure $next)
    {
        if ($request->gender != 'male') {
            return redirect('home');
        }

        return $next($request);
    }

}
We also can control either the middleware happens before or after a request. It depends the location variable $request we declare inside the middleware .

For example, the code below shows the middleware will trigger before a request ;

<?php

namespace App\Http\Middleware;

use Closure;

class BeforeMiddleware
{
    public function handle($request, Closure $next)
    {
        // Do something with your code
        return $next($request);
    }
}

While , the code below shows the middleware will runs after a request received ;

<?php

namespace App\Http\Middleware;

use Closure;

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

        // Do something with your code
        return $response;
    }
}

Registering A Middleware


Developers have an option where the middleware can be run on every HTTP request by placing them in global middleware in the $middleware property inside app  app/Http/Kernel.php directory ;


protected $middleware = [

      \App\Http\Middleware\CheckMaleGender::class
];

However , if the developers want to specify the middleware on a specific route, they can declare inside  $routeMiddleware property ;

protected $routeMiddleware = [
    ...
    'male' => \App\Http\Middleware\CheckMaleGender::class,
];
After the middleware has been assigned in  app/Http/Kernel.php directory , developers can attach them on Routes inside routes/web.php .

use App\Http\Middleware\CheckAge;

Route::get('customer/gender', function () {
    //
})->middleware(CheckMaleGender::class);

Authorization Via Middleware


Middleware also works well with authorization where develepors can authorize actions on user "role" who can access before or even incoming request.

Example of authorization :

use App\Customer;

Route::put('/customer/{id}/update', function (Customer $customer) {
    // The current user may update the post...
})->middleware('can:update,customer');

References :

https://scotch.io/tutorials/understanding-laravel-middleware
https://laravel.com/docs/5.4/middleware

No comments:

Post a Comment