Session 无法在Symfony2中检索会话数据

Session 无法在Symfony2中检索会话数据,session,symfony,session-variables,Session,Symfony,Session Variables,我使用以下代码在Symfony2中为会话编写了一个值: $session = new Session(); $session->start(); $session->set('passport', '1'); public function indexAction() { $request = $session->get('passport'); if (isset($request)) {

我使用以下代码在Symfony2中为会话编写了一个值:

    $session = new Session();
    $session->start();
    $session->set('passport', '1');
public function indexAction()
{
        $request = $session->get('passport');

        if (isset($request)) {
                    do something
        } else {
            return $this->redirect("denied.html");
        }
}
在同一页上,我使用以下代码测试该值是否已存储在会话中,并获得了完全成功:

    $request = $session->get('passport');
    print_r($request);
但是,在其中创建会话的控制器会将用户重定向到另一个控制器。在此控制器中,我有以下代码:

    $session = new Session();
    $session->start();
    $session->set('passport', '1');
public function indexAction()
{
        $request = $session->get('passport');

        if (isset($request)) {
                    do something
        } else {
            return $this->redirect("denied.html");
        }
}
但什么也没发生。屏幕变成白色,没有任何错误。我已将
$request
变量替换为以下代码:

$request = '1';
代码运行良好。就在我试图获得会话时,它不起作用。

我找到了这个答案,并尝试了第二个问题

第二个问题添加了以下代码:

$session = $this->getRequest()->getSession();
我已经添加了这个,代码现在可以工作了