Note that there are some explanatory texts on larger screens.

plurals
  1. POSessions do not work in Silex\App
    text
    copied!<p>For whatever reason, Sessions don't work in my Silex-app. I set error_reporting in my php.ini to <code>E_ALL | E_STRICT</code> and errors are displayed and logged. Nothing to see there. But for some reason no session is created and there is no file in <code>/project-root/tmp/sessions/</code> (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 <code>$app['session']</code>, <code>$request-&gt;getSession()</code> and <code>$app['request']-&gt;getSession()</code> to no avail.</p> <p>I am at a loss as to where else to look for problems...</p> <p>Here is a very simple test-app I wrote (<code>use</code> 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 <code>$app-&gt;before()</code> I check if the value is set and then pass it to twig to be displayed, e.g. login info: <code>You are logged in as {{ user.name }}</code>:</p> <pre><code>$app = new Application; $app['debug'] = true; $app-&gt;register(new SessionServiceProvider, array( 'session.storage.save_path' =&gt; dirname(__DIR__) . '/tmp/sessions' )); $app-&gt;register(new TwigServiceProvider, array( 'twig.path' =&gt; __DIR__ . '/views' )); $app-&gt;before(function (Request $request) use ($app) { // if ($app['debug'] == true) { // $request-&gt;getSession()-&gt;set('test', array('key' =&gt; 'value')); // } if ($request-&gt;hasPreviousSession() &amp;&amp; $request-&gt;getSession()-&gt;has('test')) { $test = $request-&gt;getSession()-&gt;get('test'); if ($test !== null) { $app['twig']-&gt;addGlobal('before', $test); } } }); $app-&gt;get('/', function () use ($app) { return $app['twig']-&gt;render('sessiontest.twig'); }); $app-&gt;get('/test', function () use ($app) { $app['session']-&gt;set('test', array('key' =&gt; 'value')); return $app['twig']-&gt;render('sessiontest.twig', array('get' =&gt; $app['session']-&gt;get('test'))); }); $app-&gt;run(); </code></pre> <p>sessiontest.twig looks like this:</p> <pre><code>&lt;html&gt; &lt;head&gt;&lt;title&gt;test&lt;/title&gt;&lt;/head&gt; &lt;body&gt; {% if before is defined %}before: {{ before.key }}{% endif %} {% if get is defined %}get: {{ get.key }}{% endif %} &lt;/body&gt; &lt;/html&gt; </code></pre> <p>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).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload