0
找到this question後,我得到了我的Silex會議,以至少獲得一個ID分配,但我沒有看到會話數據得到保存在不同的頁面訪問量:Silex會話不保存數據?
require(__DIR__.'/vendor/autoload.php'); // Composer autoload
$app = new Silex\Application();
$app['debug'] = true;
$app['db'] = $app->share(function() { // Database connector as a service
return new PDO('mysql:host=localhost;dbname=myawesome_db', 'user', 'paswd');
});
$app['session.db_options'] = array(
'db_table' => 'php_sessions',
'db_id_col' => 'session_id',
'db_data_col' => 'session_value',
'db_time_col' => 'session_time',
);
$app['session.storage.pdohandler'] = $app->share(function() use ($app) {
return new Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler(
$app['db'],
$app['session.db_options'],
$app['session.storage.options']
);
});
$app['session.storage'] = $app->share(function() use ($app) {
return new Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage(
$app['session.storage.options'],
$app['session.storage.pdohandler']
);
});
$app->register(new Silex\Provider\SessionServiceProvider()); // Start session management
$app['session']->start();
$app->get('/session-set', function() use ($app) {
$app['session']->set('foobar', 'test');
$app['session']->save();
$out = "User session is: ".$app['session']->getId()."<br />\n";
$out .= "Session variable is: ".var_export($app['session']->get('foobar'), true)."<br />\n";
return $out;
});
$app->get('/session-get', function() use ($app) {
$out = "User session is: ".$app['session']->getId()."<br />\n";
$out .= "Session variable is: ".var_export($app['session']->get('foobar'), true)."<br />\n";
return $out;
});
$app->run(); // Off into the sunset
的/session-set
頁面顯示foobar
值會話變量,但/session-get
頁面沒有。任何人都能幫我弄清楚爲什麼?