【问题标题】:Laravel | Error : Header may not may not contain more than a single header拉拉维尔 |错误:标头不得包含多个标头
【发布时间】:2017-05-17 10:45:24
【问题描述】:

我在我的laravel 5.4 项目中实现了Multi-Auth,但是每当我尝试从另一台设备登录时,我都会收到此错误。

Response.php 第 386 行中的 ErrorException:标头可能不包含更多 比单个标题,检测到新行

现在我已尝试查看此网站中的其他类似问题,但没有一个与我在登录控制器中所做的匹配。

这是我的登录控制器:

   class LoginController extends Controller
{


    use AuthenticatesUsers;   

    public function __construct()
    {
        $this->middleware('guest', ['except' => 'logout']);
    }

    public function username()
    {
        return 'mobile_no';
    }

    protected function redirectTo( )
    {
        $notification = array(
            'message' => 'Welcome Admin!', 
            'alert_type' => 'info',
            'title' => Auth::user()->name
        );
        return redirect('/home')->with('notification', $notification);
    }
}

我的redirecTo() 函数有什么问题?

【问题讨论】:

    标签: php laravel http http-headers laravel-5.4


    【解决方案1】:

    这个问题已经回答了here。 基本上你的方法应该返回一个字符串而不是一个重定向响应。 这是一个例子:

    namespace App\Http\Controllers\Auth;
    
    use App\Http\Controllers\Controller;
    use Illuminate\Foundation\Auth\AuthenticatesUsers;
    use Illuminate\Support\Facades\Auth;
    
    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 = '/';
    
         /**
          * Create a new controller instance.
          *
          * @return void
          */
         public function __construct()
         {
              $this->middleware('guest')->except('logout');
         }
    
         public function redirectTo(){
              return '/admin';
         }
    
    
    }
    

    但是因为您实际上需要重定向到视图并包含一些对您不起作用的数据。您需要的是一起覆盖重定向功能并创建您自己的 . 这是您需要做的。您可以从这里复制整个课程,它应该可以开箱即用:)。这是代码。干杯。

    namespace App\Http\Controllers\Auth;
    
    use App\Http\Controllers\Controller;
    use Illuminate\Foundation\Auth\AuthenticatesUsers;
    use Illuminate\Support\Facades\Auth;
    
    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 = '/';
    
         /**
          * Create a new controller instance.
          *
          * @return void
          */
         public function __construct()
         {
              //this should not be included
              //$this->middleware('guest')->except('logout');
         }
    
         //public function redirectTo(){
         //     return '/admin';
         //}
         protected function authenticated()
     {
          $notification = array(
              'message' => 'Welcome Admin!',
              'alert_type' => 'info',
              'title' => Auth::user()->name
          );
          return redirect('/home')->with('notification', $notification);
     }
    
    }
    

    【讨论】:

    • 您能否澄清一下我为什么要禁用$this->middleware('guest')->except('logout');?没有它,登录的用户可以访问login 页面,这不应该是期望的行为吗?
    • 尝试启用它。它不是必须的。它应该被禁用,因为该代码也会重定向并且您想要覆盖该行为。您也可以随时编辑该中间件,以确保它根据条件正确重定向,而不仅仅是“/home”
    猜你喜欢
    • 1970-01-01
    • 2015-01-22
    • 1970-01-01
    • 1970-01-01
    • 2018-01-06
    • 2016-02-08
    • 2018-11-24
    • 1970-01-01
    • 2018-04-04
    相关资源
    最近更新 更多