[英]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
. 我正在尝试设置一个规则,如果用户是
Worker
或Auditor
并登录,则该用户将被重定向到/post
否则将被重定向到/charts
。 In my default LoginController.php
, I tried to do something like this: 在我的默认
LoginController.php
,我尝试执行以下操作:
$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)
语法错误,意外的'$ redirectAuth'(T_VARIABLE),预期函数(T_FUNCTION)或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: 在@Karl Hill的帮助下,我能够创建自己的自定义登录名:
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. 在LoginController中,删除以下行。
protected $redirectTo = '/home';
Then add a new method redirectTo()
to the LoginController. 然后将新方法
redirectTo()
添加到LoginController。 The redirectTo()
method takes precedence over the redirectTo
property. redirectTo()
方法优先于redirectTo
属性。
public function redirectTo()
{
$type = auth()->user()->user_type();
// Check user type
switch ($type) {
case 'Worker':
return '/post';
case 'Auditor':
return '/charts';
default:
return '/login';
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.