简体   繁体   中英

Customized redirect login Laravel

I am trying set a rule where if a user is a Worker or Auditor and logs in, he will be redirected to /post otherwise he will be redirected to /charts . In my default LoginController.php , I tried to do something like this:

$redirectAuth = Auth::user()->user_type = 'Worker' || Auth::user()->user_type = 'Auditor'
        ? '/post'
        : '/charts';

protected $redirectTo = $redirectAuth;

I get this error:

syntax error, unexpected '$redirectAuth' (T_VARIABLE), expecting function (T_FUNCTION) or const (T_CONST)

Am I typing something wrong here? I appreciate any answer thanks.

EDIT:

With the assistance of @Karl Hill, I was able to create my customized login:

public function redirectTo()
{
    $userType = auth()->user()->user_type;

    return $userType == 'Worker' || $userType == 'Auditor' ? '/charts' : 'brethren';
}

but of course, this will also work:

switch ($type) {
    case 'Worker':
    case 'Auditor':
        return '/charts';
    default:
        return '/post';
}

Whichever your coding preference is.

In LoginController, remove the following line.

protected $redirectTo = '/home'; 

Then add a new method redirectTo() to the LoginController. The redirectTo() method takes precedence over the redirectTo property.

public function redirectTo()
{
    $type = auth()->user()->user_type();

    // Check user type
    switch ($type) {
        case 'Worker':
            return '/post';
        case 'Auditor':
            return '/charts';
        default:
            return '/login';
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM