Laravel 5.3 登录控制器 - 标头不能包含多个标头,检测到换行符。

13

更改默认的LoginController登录后重定向时,我遇到了问题,出现了以下错误: 在Response.php的第339行发生ErrorException: 头部不能包含多个标头,检测到新行

我已经尝试了一切,但它仍然不起作用。代码如下:

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 属性保留不变应该是可以的。

而且,我已经检查过,代码实际上已经到达了最后一个条件。


1
相关问题:https://dev59.com/cl_Va4cB1Zd3GeqPUIm5 或 http://stackoverflow.com/questions/17699303/errorexception-warning-header-may-not-contain-more-than-a-single-header-new-l - Pipe
嗯,是的,但我看不出它在哪个部分重定向了两次。 - Aarón Gutiérrez
@AarónGutiérrez 我在这个问题中看不到任何重定向的部分。我只看到一个函数,如果被调用,并且如果它的结果作为响应返回,将会导致重定向。也许值得检查哪里生成了该响应以及其中包含了什么。 - apokryfos
重定向部分是 return redirect('/copy/dashboardCopy');,正如我所提到的,代码已经到达了那个部分... Laravel 没有说要检查其他地方 [https://laravel.com/docs/5.3/authentication#authentication-quickstart],它只是说要添加方法并执行您想要的逻辑。 - Aarón Gutiérrez
4个回答

63

redirectTo方法应该返回一个URL路径,而不是重定向响应(Response)。

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

5
谢谢!我之前使用的是return redirect()->route('series');而不是return route('series'); - JCarlosR
成功地将 RedirectIfAuthenticated 中的重定向复制并粘贴到控制器中,你刚刚拯救了我的理智。 - Honest Objections

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']);
}
}

0
public $redirectTo = '/lender/home';

protected function redirectTo()
{
    if(\Auth::guard('lender')->check()){
      $this->redirectTo = '/lender/home';
      return $this->redirectTo;
    }
}

0
在 app/Http/Middleware/Authenticate.php 文件中,注释掉这部分内容。
protected function redirectTo($request)
         {
             if (! $request->expectsJson()) {
                return view('admin.auth.login');

             }
         }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接