ログイン後にデフォルトの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 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']);
}
}
メソッドredirectToは、リダイレクトレスポンスではなく、URLパスを返す必要があります。
...
protected function redirectTo()
{
if(\Auth::user()->hasRole('copy')){
return '/copy/dashboardCopy';
}
}
...
App/Http/MiddlewareのAuthenticate.phpのこの部分にコメントする
protected function redirectTo($request)
{
if (! $request->expectsJson()) {
return view('admin.auth.login');
}
}