YouTube Icon

Code Playground.

How to middleware work with route in Laravel 5

CFG

How to middleware work with route in Laravel 5

Middleware provides a convenient method for entering the program to process HTTP requests.

I've built three middleware, and by working I have routes.

  1. UserMiddleware
  2. AdminMiddleware
  3. SupperAdminMiddleware

UserMiddleware

<?php

namespace App\Http\Middleware;
use Auth;
use Closure;

class UserMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if(Auth::check())
        {
            if(Auth::user()->is_admin != '0' && Auth::user()->is_admin != '1' && Auth::user()->is_admin != '2')
            {
                Auth::logout();    
                return redirect('/')->with('message', 'Email and Password is wrong!');
            }
            return $next($request);
        }
        else
        {
            Auth::logout();
            return redirect('/');
        }
    }
}

AdminMiddleware

<?php

namespace App\Http\Middleware;
use Auth;
use Closure;

class AdminMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {

        if(Auth::check())
        {
            if(Auth::user()->is_admin == '0' )
            {
               return redirect('dashboard');
            }
            return $next($request);
        }
        else
        {   
            return redirect('/')->with('message', 'Email and Password is wrong!');
        }
    }
}

SupperAdminMiddleware

<?php

namespace App\Http\Middleware;

use Closure;

class SupperAdminMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if(Auth::check())
        {
            if(Auth::user()->is_admin == '0' && Auth::user()->is_admin == '1')
            {
                    return redirect('dashboard');
            }
            return $next($request);
        }
        else
        {
            return redirect('/')->with('message', 'Email and Password is wrong!');
        }
    }
}

Now, call with app/http/kernel.php

protected $routeMiddleware = [
        'superadmin' => \App\Http\Middleware\SupperAdminMiddleware ::class,
        'admin' => \App\Http\Middleware\AdminMiddleware ::class,
        'user' => \App\Http\Middleware\UserMiddleware ::class,
];

Finally route call in middleware three portion show below code.

/* Admin, Supper Admin, User only Routes */ 
Route::group(['middleware' => 'user'], function(){

	Route::get('dashboard', [
	    'as' => 'dashboard',
	    'uses' => 'DashboardController@index'
	]);
});

/* Admin, Supper Admin only Routes */ 
Route::group(['middleware' => 'admin'], function(){

    //user portion
    Route::get('add-user', [
        'as' => 'add-user',
        'uses' => 'AddUserController@AddUser'
    ]);

    Route::get('view-user', [
        'as' => 'view-user',
        'uses' => 'AddUserController@ViewUser'
    ]);
});

/* Supper Admin only Routes */ 
Route::group(['middleware' => 'superadmin'], function(){

});	

In above Example code Supper Admin, Admin and User wise route and middleware work.




CFG