Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sessions do not work in Silex\App

Tags:

php

session

silex

For whatever reason, Sessions don't work in my Silex-app. I set error_reporting in my php.ini to E_ALL | E_STRICT and errors are displayed and logged. 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). Switching to PdoSessionStorage, to rule out problems with read/write-permissions on the filesystem, brought no results either. I also tried switching between $app['session'], $request->getSession() and $app['request']->getSession() to no avail.

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). 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, e.g. login info: 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:

<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).

like image 777
dbrumann Avatar asked Mar 16 '12 09:03

dbrumann


1 Answers

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. 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. 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.

like image 180
igorw Avatar answered Oct 23 '22 02:10

igorw