简体   繁体   English

会话在Silex \\ App中不起作用

[英]Sessions do not work in Silex\App

For whatever reason, Sessions don't work in my Silex-app. 无论出于何种原因,Sessions不能在我的Silex-app中运行。 I set error_reporting in my php.ini to E_ALL | E_STRICT 我将php.ini中的error_reporting设置为E_ALL | E_STRICT E_ALL | E_STRICT and errors are displayed and logged. 显示并记录E_ALL | E_STRICT和错误。 Nothing to see there. 没什么好看的。 But for some reason no session is created and there is no file in /project-root/tmp/sessions/ (and also not, when using the default session.save_path). 但由于某种原因,没有创建会话,并且/project-root/tmp/sessions/没有文件(当使用默认的session.save_path时也没有)。 Switching to PdoSessionStorage, to rule out problems with read/write-permissions on the filesystem, brought no results either. 切换到PdoSessionStorage,以排除文件系统的读/写权限问题,也没有带来任何结果。 I also tried switching between $app['session'] , $request->getSession() and $app['request']->getSession() to no avail. 我也尝试在$app['session']$request->getSession()$app['request']->getSession()切换无效。

I am at a loss as to where else to look for problems... 我不知道在哪里寻找问题......

Here is a very simple test-app I wrote ( use is omitted to save space). 这是我写的一个非常简单的测试应用程序(省略use以节省空间)。 Basically this test shows what I try to achieve in my actual app. 基本上这个测试显示了我在实际应用中尝试实现的功能。 I want to store an array in the session. 我想在会话中存储一个数组。 In $app->before() I check if the value is set and then pass it to twig to be displayed, eg login info: You are logged in as {{ user.name }} : $app->before()我检查是否设置了值,然后将其传递给twig进行显示,例如登录信息: You are logged in as {{ user.name }}

$app = new Application;
$app['debug'] = true;
$app->register(new SessionServiceProvider, array(
    'session.storage.save_path' => dirname(__DIR__) . '/tmp/sessions'
));
$app->register(new TwigServiceProvider, array(
    'twig.path' => __DIR__ . '/views'
));

$app->before(function (Request $request) use ($app) {
//    if ($app['debug'] == true) {
//        $request->getSession()->set('test', array('key' => 'value'));
//    }
    if ($request->hasPreviousSession() && $request->getSession()->has('test')) {
        $test = $request->getSession()->get('test');
        if ($test !== null) {
            $app['twig']->addGlobal('before', $test);
        }
    }
});

$app->get('/', function () use ($app) {
    return $app['twig']->render('sessiontest.twig');
});
$app->get('/test', function () use ($app) {
    $app['session']->set('test', array('key' => 'value'));
    return $app['twig']->render('sessiontest.twig',
        array('get' => $app['session']->get('test')));
});
$app->run();

sessiontest.twig looks like this: sessiontest.twig看起来像这样:

<html>
<head><title>test</title></head>
<body>
    {% if before is defined %}before: {{ before.key }}{% endif %}
    {% if get is defined %}get: {{ get.key }}{% endif %}
</body>
</html>

When going to / nothing is displayed, when going to /test only "get: test" is displayed (but not actually stored, as neither returning to / nor refreshing triggers before). 当显示/不显示时,当进入/测试时,仅显示“get:test”(但实际上没有存储,因为之前都没有返回/也没有刷新触发器)。

Right now you need to start the session manually: 现在您需要手动启动会话:

$app->before(function ($request) {
    $request->getSession()->start();
});

There are tickets about this in the silex tracker, I haven't had time to think about it yet. 在silex跟踪器中有关于此的门票,我还没有时间考虑它。 The problem is that if you do this, you will make a session for every user. 问题是如果你这样做,你将为每个用户进行一次会话。 Every using having a session, means every user gets cookies. 每个使用会话的人都意味着每个用户都会获得cookie。 Which is something you may not want. 这是你可能不想要的东西。 Which is the reason why it is not being started automatically at this point. 这就是为什么它不会在此时自动启动的原因。

I hope that clears things up a bit. 我希望稍微澄清一下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM