Laravel 5.2.11,会话无法正常工作,未设置会话Cookie [英] Laravel 5.2.11, sessions are not working, session cookie not being set
问题描述
我是Laravel的新手,我已经了解了模型,视图,刀片,控制器和路由以及它们如何协同工作.到目前为止,一切进展顺利.
I'm new to Laravel, I have learned about the models, views, blade, controllers and routes and how they work together. So far everything has been working smoothly.
我在会话上遇到了麻烦.
I'm having trouble with sessions though.
当我使用Laravel随附的AuthController并通过POST请求命中auth/register时,我注册的用户的数据确实插入到用户表中(使用mysql),并且确实获得了响应"Location"标头重定向到/就像开箱即用.它会像应该的那样重定向.但是在相同的响应中,没有设置/发送"Set-Cookie"头. Laravel的会话部分对我来说无法正常工作.对于POST进行身份验证/登录是相同的,它可以正确进行身份验证并重定向到个人资料页面,但响应中不会发送回任何会话Cookie.
When I use the AuthController that comes with Laravel and hit auth/register with a POST request, the data for the user that I register does get inserted into the users table (using mysql) and I do get back a response with the "Location" header redirecting to / like it does out of the box. It redirects like it should. But in the same response there is no "Set-Cookie" header set/sent. The session part of Laravel is not working properly for me. This is the same for a POST to auth/login, it authenticates properly and redirects to the profile page but no session cookie is sent back in the response.
我正在使用:
- Laravel 5.2.11
- PHP 5.5.9
- xubuntu 14.04(Ubuntu)
- Linux内核3.19.0-42-通用
- Composer 1.0
已安装Laravel所需的所有php模块.我正在使用php的内置Web服务器运行该应用程序.我用sudo运行它.我运行的确切命令是这样的:
All of the php modules that Laravel requires are installed. I'm running the app with php's built in web server. I run that with sudo. The exact command I run is this:
sudo php -S localhost:8888 -t public/
所有路线都得到了正确的响应.
All routes are being responded to properly.
我尝试了通过laravel可执行文件和composer create-project来完成安装文档建议的两种Laravel安装方法.仍然没有cookie设置任何一种方式.我已经制作了laravel项目mod 777的所有文件和目录.如果有任何区别,请在.env中设置应用程序密钥.
I have tried both ways of installing Laravel that the installation docs recommend, through the laravel executable and composer create-project. Still no cookies set either way. I have made all the files and directories of the laravel project mod 777. The app key is set in .env if that makes any difference.
config/session.php文件正在使用文件驱动程序进行会话. 设置会话后,storage/framework/sessions目录中没有会话文件.
The config/session.php file is using the file driver for the session. There are no session files in the storage/framework/sessions directory after setting a session.
当我尝试自行设置会话功能时,就像文档中所述的那样:
When I try setting a session myself with the session function like it states in the docs:
session(['sesskey' => 'somevalue']);
再次在响应中未发送"Set-Cookie"标头,并且未创建会话文件.没有错误消息,我也应该添加.
Again no "Set-Cookie" header is sent in the response and no session file is created. There are no error messages reported back either I should add.
当我使用上述会话功能设置会话密钥时,我可以获取该值,然后像这样将其回传给浏览器:
When I do set a session key with the session function like above I can get that value back however and echo it back to the browser like so:
echo session('sesskey');
因此,它似乎至少可以将其保存在php的内存中.
So it does seem to save it at least in php's memory.
当我尝试使用withCookie方法设置cookie时,使用Set-Cookie标头集确实得到了正确的响应:
When I try setting a cookie using the withCookie method, I do get the proper response with the Set-Cookie header set:
return response()->view('welcome')->withCookie(cookie("test", "val" , 3600));
我试图沿着发光的兔子洞走下去,看是否可以找到问题,但这在我的自动柜员机上有点过头.
I tried going down the illuminate rabbit hole to see if I could find a problem but that is a bit over my head atm.
任何帮助都会非常有用,谢谢!
Any help would be much appriciated, thanks!
推荐答案
在laravel 5.2中,您需要使用网络"中间件来解决问题,诸如此类
in laravel 5.2 you need to use "web" middleware for your problem,like that
Route::group(['middleware' => ['web']], function () {
//
});
这篇关于Laravel 5.2.11,会话无法正常工作,未设置会话Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!