Laravel 会话中间件损坏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36060275/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel Session Middleware broken
提问by DehMotth
On my local system everything works fine, but after deploying Laravel 5.2 on our test system it looks like the session middleware is broken. Can someone help here?
在我的本地系统上一切正常,但是在我们的测试系统上部署 Laravel 5.2 后,会话中间件似乎已损坏。有人可以帮忙吗?
Argument 1 passed to Illuminate\Session\Middleware\
StartSession::addCookieToResponse() must be an instance of
Symfony\Component\HttpFoundation\Response, boolean given, called in
... /httpdocs/service/vendor/laravel/framework/src/Illuminate/Session
/Middleware/StartSession.php on line 72 and defined
The global middlewares:
全局中间件:
protected $middleware = [
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\CORSMiddleware::class,
\LucaDegasperi\OAuth2Server\Middleware\OAuthExceptionHandlerMiddleware::class
];
回答by Leonardo Beal
I had the same problem. When investigating, I discovered that at some point in my code, I used return
.
我有同样的问题。在调查时,我发现在我的代码中的某个时候,我使用了return
.
It turns out (as you can see in the end of the handle method) that after executing the handle method you should always call return $next($request);
.
事实证明(正如您在 handle 方法的末尾所看到的)在执行 handle 方法之后,您应该始终调用return $next($request);
.
回答by T.J.
I had similar problem in one of mine middleware (v5.8). 'Call to a member function SetCookie() on null', 'Add the CSRF token to the response cookies'
我在我的一个中间件 (v5.8) 中遇到了类似的问题。'在 null 上调用成员函数 SetCookie()', '将 CSRF 令牌添加到响应 cookie'
This was my code, working fine in 5.2, but failed in Laravel 5.8:
这是我的代码,在 5.2 中运行良好,但在 Laravel 5.8 中失败:
return view('pages.my_page')->with('data', $data);
changed to:
变成:
return response()->view('pages.my_page', ['data' => $data]);
Cheers!
干杯!
回答by neochief
Well the addCookieToResponse method in the Illuminate\Session\Middleware\StartSession class is wanting a Response object as the first param. Make sure that you return one in all of your routes.
Illuminate\Session\Middleware\StartSession 类中的 addCookieToResponse 方法需要一个 Response 对象作为第一个参数。确保在所有路由中返回一个。
Here's a possible quick fix, change it to fit your case.
这是一个可能的快速修复,更改它以适合您的情况。
Before:
前:
Route::get('hi', function() {
return 'hi';
});
After:
后:
Route::get('hi', function() {
return response('hi');
});
回答by Matteus Barbosa
In my case it was just cache. try running
就我而言,它只是缓存。尝试跑步
php artisan config:cache
回答by George_kane
To all the people coming for this error, it's the cookie that fails.
对于所有为此错误而来的人来说,是 cookie 失败了。
So the fastest fix is to use another browser.
Go to settings in your browser and find the cookie and delete it.
所以最快的解决方法是使用其他浏览器。
转到浏览器中的设置,找到 cookie 并将其删除。