Bladephp: Now with 8+ codeigniter, php, and laravel project 8+ php project From $37/Month From Rs. 2499/Month

How to middleware work with route in Laravel 5

by
Category:laravel
laravel
How to middleware work with route in Laravel 5

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

I have created three middleware and provides routes through working.

  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.


0 Comments


Html code work in comment box


Related Post