Laravel会话开箱即用 [英] Laravel session out the box not working

查看:160
本文介绍了Laravel会话开箱即用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个新的laravel安装,我要做的第一件事就是确认会话是否正常工作

I've created a new laravel installation and the first thing I'm trying to do it just confirm sessions are working:

Route::get('/', function () {

    $value = Request::session()->get('key');
    if (is_null($value)) {
        Request::session()->set('key', md5(rand()));
    }
    dd(Request::session()->get('key'));

    return view('welcome');
});

我希望在这里存储会话密钥",然后在下一次刷新时,它将每次输出(dd)相同的密钥值.但是,事实并非如此.每次键"都会得到不同的值.所以我猜会话数据没有被存储吗?

I'm hoping here that session 'key' is stored, then, on the next refresh, it will output (dd) the same key value each time. However, it doesn't. I get a different value for 'key' each time. So I guess session data is not being stored?

如果有帮助,下面是config/session.php内容:

If it helps, below is the config/session.php content:

'driver' => env('SESSION_DRIVER', 'file'),
...
'files' => storage_path('framework/sessions'),
...

我的.env文件如下:

My .env has the following:

...
SESSION_DRIVER=file
...

文件夹的权限为:

$ ll /var/www/o-eco/website/storage/framework
total 24
drwxrwxr-x 1 ubuntu www-data 4096 Oct  3 02:33 ./
drwxrwxr-x 1 ubuntu www-data 4096 Oct  3 02:33 ../
drwxrwxr-x 1 ubuntu www-data 4096 Oct  3 02:33 cache/
-rw-rw-r-- 1 ubuntu www-data  103 Oct  3 02:33 .gitignore
drwxrwxr-x 1 ubuntu www-data 4096 Jan 13 10:59 sessions/
drwxrwxr-x 1 ubuntu www-data 4096 Jan 13 10:59 views/

$ ll /var/www/o-eco/website/storage/framework/sessions
total 16
drwxrwxr-x 1 ubuntu www-data 4096 Jan 13 10:59 ./
drwxrwxr-x 1 ubuntu www-data 4096 Oct  3 02:33 ../
-rw-rw-r-- 1 ubuntu www-data  258 Jan 13 10:59 5xqzOeb6f5lLKyvZIwOeonutkmluREfcaQ5owTNE
-rw-rw-r-- 1 ubuntu www-data   14 Oct  3 02:33 .gitignore

注意:这一次,我在一个无聊的盒子里进行开发,因此所有文件都是ubuntu:www-data.另外,我什至尝试将会话目录/文件设置为777,但仍然没有区别.

Note: this time, I'm developing within a vagrant box so all files are ubuntu:www-data. Also, I've even tried setting the sessions dir/files to 777 but still no difference.

我还有两个cookie:XSRF-TOKEN和laravel_session.每次刷新时,它们都保持不变.

Also, I have two cookies: XSRF-TOKEN and laravel_session. These both remain unchanged on each refresh.

推荐答案

在放置后只需使用save();即可

只需尝试

Session::put('key', md5(rand())); 
Session::save();

编辑

已删除dd()

Route::get('/', function () {

    $value = Request::session()->get('key');
    echo $value;
    if (is_null($value)) {
        Request::session()->set('key', md5(rand()));
    }

    return view('welcome');
});

这篇关于Laravel会话开箱即用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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