Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Bridging the gap between standard $_SESSION and HttpFoundation Session with Silex.
<?php
// Register all other Silex Providers
$app->register(new Silex\Provider\SessionServiceProvider, array(
'cookie_lifetime' => 86400
));
/**
* LegacyStorage for setting Base SESSION keys into Symfony Session
*/
$app['session.storage'] = $app->share(function ($app) {
return new Session\Storage\LegacySessionStorage();
});
$app['session']->start();
<?php
namespace Session\Storage;
use Symfony\Component\HttpFoundation\Session\Storage\PhpBridgeSessionStorage;
/**
* Session storage that avoids using _sf2_attributes subkey in the $_SESSION
* superglobal but instead it uses the root variable.
*
* @link(GIST, https://gist.github.com/tcz/9756112)
*/
class LegacySessionStorage extends PhpBridgeSessionStorage
{
const SYMFONY_SESSION_STORAGE_KEY = '_sf2_attributes';
const SYMFONY_SESSION_FLASH_KEY = '_sf2_flashes';
const SYMFONY_SESSION_METADATA_KEY = '_sf2_meta';
/**
* @inheritdoc
*/
protected function loadSession(array &$session = null)
{
if (null === $session) {
$session = &$_SESSION;
}
parent::loadSession($session);
foreach ($this->bags as $bag) {
$key = $bag->getStorageKey();
if (self::SYMFONY_SESSION_STORAGE_KEY === $key)
{
$bag->initialize($session);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment