Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Laravel 5.0 Creating Custom Middleware For Different Roles

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 779
    Comment on it

    Laravel provide us the facility to create our custom middleware for user's authentication. We can create different MIddleware for different roles. To accomplish it we need to follow the following steps.

    Step 1st:- Creating new Middleware for role "company" under /app/Http/Middleware/CompanyMiddleware.php

    <?php
        namespace App\Http\Middleware;
        use Closure;
        class CompanyMiddleware {
            /**
             * Handle an incoming request.
             *
             * @param  \Illuminate\Http\Request  $request
             * @param  \Closure  $next
             * @return mixed
             */
            public function handle($request, Closure $next) {
                if (!$request->user()->hasRole('company')) {
                    return redirect()->guest('/');
                }
                return $next($request);
            }
        }
    

    Step 2nd:- Creating Middleware for role "user" under /app/Http/Middleware/EmployeeMiddleware.php

    <?php
    namespace App\Http\Middleware;
    use Closure;
    class EmployeeMiddleware {
        /**
         * Handle an incoming request.
         *
         * @param  \Illuminate\Http\Request  $request
         * @param  \Closure  $next
         * @return mixed
         */
       public function handle($request, Closure $next)
        {
            if ($request->user()->hasRole('company')) {
                return redirect()->guest('admin/employee');
            }
            return $next($request);
        }
    }
    

    Step 3rd :- Creating method in the user model to check for the role.

    public function hasRole($role)
    {
        return ($this->role == $role) ? true : false;
    }
    

    Step 4th :- Registering Middleware under the Kernel.php file

    protected $routeMiddleware = [
            ......
                    'company' => 'App\Http\Middleware\CompanyMiddleware',
                    'employee' => 'App\Http\Middleware\EmployeeMiddleware',
        ];
    

    Step 5th :- How to use custom middleware

    Route::group(["middleware" => ["auth",  "employee"]], function() {
        Route::get("inspirations/{id?}/{randomize?}/{slug?}", "InspirationsController@index");
       .......
    });
    
    Route::group(["middleware" => ["auth",  "company"]], function() {
        Route::get("admin/employee/create", "Admin\EmployeesController@create");
       .......
    });
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: