Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jkoech's avatar

Laravel 5 Authentication and Roles

Hello, I am starting up a new project using Laravel 5 and Zizaco Entrust for roles. I have setup everything and would like to know how to redirect a user based on their role to the appropriate route. I.e if say, I have a super admin, once they login they should be redirected to superadmin/dashboard route.

Where can I do they redirection?

0 likes
6 replies
davorminchorov's avatar

Create a middleware class.


namespace App\Http\Middleware;

class RolesMiddleware implements Middleware {

    public function handle($request, Closure $next)
    {
    // Perform Action
        foreach ($this->roles as $role)
        {
            if ($role == 'super admin')
            {
                redirect('superadmin.dashboard');
            }
        }

            return $next($request);
    }
}

This should give you an idea.

1 like
jkoech's avatar

Thank you @Ruffles. I realized I could just customize RedirectIfAuthenticated Middleware and it worked well.

1 like
tgif's avatar

@jkoech are you now going to forgo Zizaco Entrust and simply use the middleware?

jkoech's avatar

@csuarez yes, I did stick with the middleware, I found out that it was really easy stuff.

@ptgokulrajan basically, I didn't change RedirectIfAuthenticated, I just created a new IsRole middleware, i.e IsAdmin.php with something similar to the below:

<?php namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\RedirectResponse;
class IsAdmin {
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $user = $request->user();
        if ($user && $user->isAdmin())
        {
            return $next($request);
        }
        return new RedirectResponse(url('/'));
    }
}

Please or to participate in this conversation.