2013-04-10 98 views
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頁面沒有。任何人都能幫我弄清楚爲什麼?

回答

0

我的解決方案原來是PDO數據庫連接沒有正確設置(其中一個列名是錯誤的)。但是,默認情況下,PDO MySQL連接在出錯時不會拋出異常,它只是設置ErrorInfo()的詳細信息。一旦我打開了針對我的PDO連接的異常拋出,我發現了錯誤並修復了它。我還向Symfony提交了一個補丁,以確保會話PDO對象啓用了異常拋出功能,以避免其他人觸及此問題。