Laravel 5 - 会话不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29346743/
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 5 - session doesn't work
提问by Limon Monte
Here's config/session.php
:
这是config/session.php
:
return [
'driver' => 'file',
'files' => storage_path().'/framework/sessions',
];
My storage/framework/sessions
have 755 permissions.
我storage/framework/sessions
有 755 个权限。
When I put these 2 line in my controller
当我将这两行放在我的控制器中时
Session::set('aa', 'bb');
dd(Session::get('aa'));
I receive expected "bb"
output. But if I comment first line:
我收到预期的"bb"
输出。但如果我评论第一行:
// Session::set('aa', 'bb');
dd(Session::get('aa'));
and refresh page, I still expecting "bb"
but getting null
.
并刷新页面,我仍然期待"bb"
但得到null
.
Also, storage/framework/sessions
is empty.
还有,storage/framework/sessions
是空的。
What should I do to make Session working?
我应该怎么做才能使 Session 工作?
回答by Bogdan
Laravel 5 handles sessions via a middleware class called StartSession
. More importantly, this middleware is a TerminableMiddleware
and the code that actually saves the data (in your case to the session file) is located in the terminate
method, which is run at the end of the request lifecycle:
Laravel 5 通过一个名为StartSession
. 更重要的是,这个中间件是一个TerminableMiddleware
,实际保存数据(在你的情况下保存到会话文件)的代码位于terminate
方法中,该方法在请求生命周期结束时运行:
public function terminate($request, $response)
{
if ($this->sessionHandled && $this->sessionConfigured() && ! $this->usingCookieSessions())
{
$this->manager->driver()->save();
}
}
When calling dd(Session::get('aa'));
the request is being interrupted before the terminate
method of the middleware can be called.
当调用dd(Session::get('aa'));
请求被中断之前terminate
,中间件的方法可以被调用。
Funnily enough, the Laravel Middleware Documentationactually explains Terminable Middleware logic by giving the Laravel StartSession
middleware as an example:
有趣的是,Laravel 中间件文档实际上以 LaravelStartSession
中间件为例解释了可终止中间件的逻辑:
For example, the "session" middleware included with Laravel writes the session data to storage afterthe response has been sent to the browser.
例如,Laravel 中包含的“会话”中间件在响应发送到浏览器后将会话数据写入存储。
That being said, try using var_dump()
instead of using dd()
.
话虽如此,请尝试使用var_dump()
而不是使用dd()
。
回答by arash peymanfar
With laravel 5.*, you must change the kernel file like bellow:
使用 laravel 5.*,您必须像下面这样更改内核文件:
'api' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Session\Middleware\StartSession::class,
'throttle:60,1',
'bindings',
],
then go to storage/framework/session folder and change the permission to 755 if it has another amount, then delete all files in your storage/framework/session path, use your code again to put something in a session, watch the storage/framework/session folder.
然后转到 storage/framework/session 文件夹并将权限更改为 755(如果还有其他数量),然后删除 storage/framework/session 路径中的所有文件,再次使用您的代码将某些内容放入会话中,观察 storage/framework /会话文件夹。
If your session work you can see the weird long file that belong to session right now, and you are done!
如果您的会话工作正常,您现在可以看到属于会话的奇怪的长文件,您就完成了!
If your problem is not yet solved, go to config/session and change:
如果您的问题尚未解决,请转到 config/session 并更改:
'driver' => env('SESSION_DRIVER', 'file')
to another predefined amount like:
到另一个预定义的金额,如:
'driver' => env('SESSION_DRIVER', 'array'),
or even
甚至
'driver' => env('SESSION_DRIVER', 'database'),
and finally if you have an empty folder of storage/framework/session, you still have a problem for sure !!!
最后,如果你有一个空文件夹 storage/framework/session,你肯定还是有问题!!!