我试图像这样开始(或访问)一个会话:
$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