我試着去啓動(或訪問),像這樣一個會話:Symfony 2會話頭文件已發送?
$session = new Session();
echo $session->getId();
其中根據文檔應該是所有我需要的,因爲會得到自動啓動(http://symfony.com/doc/master /components/http_foundation/sessions.html):
While it is recommended to explicitly start a session, a sessions will actually start
on demand, that is, if any session request is made to read/write session data.
儘管如此,即時得到錯誤
Failed to start the session because headers have already been sent.
繼承人的原始控制器這就是調用服務:
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Bundle\FrameworkBundle\Routing\Router;
use Symfony\Component\HttpFoundation\Response;
$auth = $this->get('authentication');
$user_id = $auth->getUserId();
然後是getUserId功能:
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Session;
public function getUserId() {
$session = new Session();
echo $session->getId();
如果我改變getUserId看起來像這樣:
public function getUserId() {
$session = $this->getRequest()->getSession();
echo $session->getId();
我得到的錯誤:
Call to a member function get() on a non-object
在嘗試訪問會話之前,您是否向瀏覽器發送過任何輸出? –
@ KevinM1除非Symfony默認設置爲 –
代碼結構中的代碼位於Controller中? – Alex