Laravel 5.3 LoginController - Header可能不包含多个标题,检测到新行

Aar*_*rez 5 php redirect login laravel-5.3

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']);
}
}
Run Code Online (Sandbox Code Playgroud)

根据Laravel文档,该方法的优先级高于属性,因此我认为保留class属性是可以的.

而且,我已经检查过,代码实际上达到了最后的状态.

Oua*_*ilB 35

redirectTo方法应返回url路径,而不是Redirect响应.

...
protected function redirectTo()
{
    if(\Auth::user()->hasRole('copy')){
        return '/copy/dashboardCopy';
    }       
}
...
Run Code Online (Sandbox Code Playgroud)

  • 谢谢!我使用`return redirect() - > route('series');`而不是`return route('series');` (2认同)

Aar*_*rez 5

我刚刚解决了它替换原始代码,

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']);
}
}
Run Code Online (Sandbox Code Playgroud)