我在登录后更改默认的LoginController
重定向时遇到问题,我得到了一个ErrorException in Response.php line 339:
Header may not contain more than a single header, new line detected
我已经尝试了一切,但它只是不起作用,代码是:
class LoginController extends Controller
{
protected $redirectTo = '/home';
protected function redirectTo()
{
if (\Auth::check()) {
$user_id = \Auth::id();
$usuario = users::where('id','=',$user_id)->first();
if($usuario->hasRole('copy')){
return redirect('/copy/dashboardCopy');
}
}
}
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest', ['except' => 'logout']);
}
}
根据Laravel文档,该方法的优先级高于属性,因此我认为保留class属性是可以的。
而且,我已经检查过,代码实际上达到了最后的状态。
redirectTo方法应返回url路径,而不是Redirect响应。
...
protected function redirectTo()
{
if(\Auth::user()->hasRole('copy')){
return '/copy/dashboardCopy';
}
}
...
我刚刚解决了它取代原始代码的问题
class LoginController extends Controller
{
/*
|--------------------------------------------------------------------------
| Login Controller
|--------------------------------------------------------------------------
|
| This controller handles authenticating users for the application and
| redirecting them to your home screen. The controller uses a trait
| to conveniently provide its functionality to your applications.
|
*/
use AuthenticatesUsers;
/**
* Where to redirect users after login.
*
* @var string
*/
protected $redirectTo;
protected function redirectTo()
{
if(\Auth::user()->hasRole('copy')){
$this->redirectTo = '/copy/dashboardCopy';
return $this->redirectTo;
}
}
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest', ['except' => 'logout']);
}
}
public $redirectTo = '/lender/home';
protected function redirectTo()
{
if(\Auth::guard('lender')->check()){
$this->redirectTo = '/lender/home';
return $this->redirectTo;
}
}