Laravel5-无法使会话正常工作 [英] Laravel5 - can't get Sessions working

查看:150
本文介绍了Laravel5-无法使会话正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Laravel 5,但无法使Session工作.我在Controller里面,这是我在全新的设置和全新的控制器上完成的操作:

Im trying Laravel 5 and cant get Session working. Im inside a Controller and this is what ive done on an fresh setup and fresh controller:

\Request::session()->put('test', 'x');
var_dump(\Request::session()->get('test'));

只要正在编写会话,此方法就起作用,一旦您注释了第一行,会话值就会在下一个请求中消失.

This works as long as session is being written, and once you comment the first line there session value is gone on the next request.

类似地,香港专业教育学院尝试使用此Session::而不是Request::session(),结果仍然相同.

Similarly, ive tried this Session:: instead of Request::session() and still same result.

推荐答案

好,我自己找到了该解决方案,感谢2篇文章:

Ok ive found the solution myself thanks to 2 posts:

Laravel 5-会话无效

会话在中间件Laravel 5中不起作用

  1. 围绕Laravel的所有OMG嗡嗡声是如此的简单,让我们在雨中跳舞吧! PHP中最简单的内容(例如$_SESSION)现在需要\Request::session(),而您需要user Request.那么您需要2种不同的方法来保存并从中获取值,而不是直接使用$ _SESSION ['x']!

  1. After all the OMG buzz around Laravel being so so so simple lets dance in the rain!! the simplest thing in PHP such as $_SESSION now requires \Request::session() for which you need to user Request. then you need 2 different methods to save and get values from it instead of $_SESSION['x'] directly!!

Laravel(天才超级外星人技术)可以将会话保存在TerminableMiddleware上,而不是在现场保存,但请不要在 https://laravel.com/docs/5.2/session .因此,您将需要显式调用Session::save()

Laravel (genius super alien tech) saves session at TerminableMiddleware instead of on spot, but DOESNOT mentions it on https://laravel.com/docs/5.2/session. So you will need to explicitly call Session::save()!!

因此,您需要5行来完成$ _SESSION的1行工作!

So there you go, 5 lines to do 1 line work of $_SESSION!

这篇关于Laravel5-无法使会话正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆