会话未在Lumen PHP框架中持续存在 [英] Sessions not persisting in Lumen PHP framework

查看:65
本文介绍了会话未在Lumen PHP框架中持续存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Lumen(Laravel的作品)微框架进行项目,并且在会话中遇到了一些麻烦.我现在只是测试实现,但是遇到的问题是,当我设置会话变量然后刷新页面时,不再设置该变量.

I'm using the Lumen (by Laravel) micro-framework for a project, and I'm having some trouble with sessions. I'm just testing the implementation now, but the problem I'm experiencing is that when I set a session variable and then refresh the page, the variable is no longer set.

在我的.env文件中,我有:

In my .env file I have:

SESSION_DRIVER=cookie

我知道这已经被接受了,因为当我将其更改为memcached时,会引发错误(因为我没有设置memcached).

And I know that this is being picked up, because when I change it to memcached it throws an error (because I don't have memcached set up).

我也启用了中间件:

$app->middleware([
    'Illuminate\Session\Middleware\StartSession',
    'Illuminate\View\Middleware\ShareErrorsFromSession',
]);

然后在我的控制器中有:

Then in my controller I have:

<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class SessionController extends Controller
{
    public function index(Request $request)
    {
        $request->session()->put('email', 'test@test.com');
        $request->session()->save(); // Not sure if this is required

        var_dump($request->session()->get('email'));
        exit;

        return view('session.index', ['test' => $value]);
    }
}

该值是在我加载页面时设置的:

The value is set when I load the page:

string(13) "test@test.com"

但是当我注释掉设置变量的行,然后刷新页面时,该值为NULL:

But then when I comment out the lines that set the variable, and I then refresh the page, the value is NULL:

// $request->session()->put('email', 'test@test.com');
// $request->session()->save();

var_dump($request->session()->get('email'));
exit;

在浏览器中设置了一个cookie,但它似乎不是用于会话变量的:

A single cookie is being set in the browser, but it doesn't appear to be for the session variable:

laravel_session 2ecef0103418ca82d068ec6a6c6fbec388af9b9e    localhost   /   2015-06-22T14:59:29.856Z    55  ✓

如果我将SESSION_DRIVER设置为cookie,则实际上是在设置cookie –不管我是否实际设置了会话变量.

The cookie is actually set if I set the SESSION_DRIVER as cookie – regardless of whether or not I actually set a session variable.

我不确定这里哪里出了问题,并且我认为文档不够全面.

I'm not sure where I'm going wrong here, and I don't find the documentation very comprehensive.

谢谢

推荐答案

由于您尝试使用cookie会话存储,因此您也将需要Cookie中间件.启用的中间件应为:

Since you are trying to use the cookie session storage you'll need the cookie middleware too. The enabled middleware should be:

$app->middleware([
    'Illuminate\Cookie\Middleware\EncryptCookies',
    'Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse',
    'Illuminate\Session\Middleware\StartSession',
    'Illuminate\View\Middleware\ShareErrorsFromSession',
]);

在第一个示例中,您在创建会话后立即使用了该会话,因此您可以恢复该值.但是,由于会话cookie本身不是由代码设置的,因此当您返回到页面时,不会恢复任何会话.无论是否使用Cookie存储,始终会设置"laravel_session" cookie.我在项目中使用file存储,但仍被设置.

In your first example you use the session immediately after creating it so you are able to recover the value. However, since the session cookie itself is not set by the code, when you return to the page, no session is recovered. The "laravel_session" cookie is always set whether you use cookie storage or not. I'm using the file storage in a project and still it gets set.

另外,AFAIK,一行​​:

Also, AFAIK, the line:

$request->session()->save();

是不必要的.

文档指出:

要启用会话,您必须取消注释其中的所有中间件 bootstrap/app.php文件中的$app->middleware()方法调用.

To enable sessions, you must uncomment all of the middleware within the $app->middleware() method call in your bootstrap/app.php file.

这篇关于会话未在Lumen PHP框架中持续存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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