我试着去启动(或访问),像这样一个会话: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