2015-04-23 109 views

回答

1

invalidate()

Clears all session data and regenerates session ID. Do not use session_destroy().

這其實你想要做什麼。

來源&更多信息: http://symfony.com/doc/current/components/http_foundation/sessions.html

而且這樣的事情很容易,只需查看源進行檢查。

對於這種情況,你應該檢查SessionSessionInterface來源:

http://api.symfony.com/2.6/Symfony/Component/HttpFoundation/Session/SessionInterface.html

http://api.symfony.com/2.6/Symfony/Component/HttpFoundation/Session/Session.html

編輯。

當然,這種方法屬於Session類,因此您必須首先在控制器中訪問Session對象。

所以我們去:

http://symfony.com/doc/current/book/controller.html#managing-the-session

,我們看到如何做到這一點:

public function indexAction(Request $request) 
{ 
    $session = $request->getSession(); 

    $session->invalidate(); //here we can now clear the session. 
} 
+0

以下錯誤發生 嘗試從命名空間「主頁\ HomeBundle通話功能 「失效」 \ Controller –

+0

這是'Session'對象的一種方法,而不是'Controller',首先你必須訪問'Session'對象,看到我編輯的答案 –

+0

謝謝它解決了我的pr oblem :)一個更多的queastion ...現在我正在做這個動作開始會話 $ session = new Session(); $ this-> session-> start(); 我想寫這個$ session = new Session();在構造函數中,並在控制器的所有操作中使用它。我怎麼能做到這一點,我無法在任何地方找到它的語法。 –