Silex Bridge to Old-Style PHP Sessions ($_SESSION)

I am exploring the Silex framework. I am trying to gently evolve a very nice app from “old school PHP” to “new school PHP”. I have chosen Silex (atop Symfony) as my first framework to try. I like Silex so far because it helps me without lecturing me. I cannot rewrite my whole app overnight – I need to evolve to a Silex App, one feature at a time.

This code uses the Symfony PhpBridgeSessionStorage – and it makes me very happy.


<?php

// http://localhost:8888/silex/hello/bob

use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Storage\PhpBridgeSessionStorage;

require_once "vendor/autoload.php";

session_start();
$session = new Session(new PhpBridgeSessionStorage());
$session->start();
$app = new Silex\Application();
$app['session'] = $session;

$app->get('/hello/{name}', function ($name) use ($app) {
    echo("<pre>\n");
    // New Session
    if ( $app['session']->has('y') ) {
        $app['session']->set('y', $app['session']->get('y')+1);
    } else {
        $app['session']->set('y', 20);
    }
    print_r($app['session']->all());

    // Read and write old session
    if ( isset($_SESSION['x'])) {
        $_SESSION['x']++;
    } else {
        $_SESSION['x'] = 42;
    }
    print_r($_SESSION);

    echo("</pre>\n");
    return "<p>Hello $name</p>\n";
});

$app->run();