Thursday, October 22, 2015

Page redirection according role in laravel 5 . *>

In AuthController make a function and make a redirectPath function which will replace redirectPath function which in RedirectsUsers traits

//following custom function assign for redirection page according role
    public function redirect_to_desired_page_according_role(){
        $user = Auth::user();
        if ($user->isAdmin()) {
            return '/master';
        } elseif ($user->isTrainer()) {
            return '/public_trainings';

        } elseif ($user->isTrainee()) {
            return '/bardtrainer';

        } elseif ($user->isMonitor()) {
            return '/clients';
        }
    }


    //following function will replace the function which is in RedirectsUsers traits
    public function redirectPath()
    {
        if (property_exists($this, 'redirectPath')) {
            return $this->redirectPath;
        }

        return property_exists($this, 'redirectTo') ? $this->redirectTo : $this->redirect_to_desired_page_according_role();
    }

In RedirectIfAuthenticated.php change the handle function like following


public function handle($request, Closure $next)
    {
        if ($this->auth->check()) {
            return redirect((new AuthController())->redirect_to_desired_page_according_role());
        }

        return $next($request);
    }

No comments:

Post a Comment

css snippet for blogger code highlighting

code, .code {     display: block;     background: beige;     padding: 10px;     margin: 8px 15px; }