Symfony2会话var未在控制器上定义

时间:2016-11-28 09:34:45

标签: php symfony session

我对Symfony2会话有一个奇怪的错误,我无法解释。有时候某些变量没有定义。

我有一个像这样的控制器方法:

public function controlAction ( Request $request ) {
    $now = new \DateTime();
    $session = $request->getSession();

    $start = $request->get("start", "no");
    $end   = $request->get("end"  , "no");

    if ( $start === "ok" ) {
        $this->logger->info("[START]");
        $session->set("sum", 0);
        $session->set("starts_at", $now);
    } elseif ( $end === "ok" ) {
        $this->logger->info("[END]");
        $tmp = $session->get("sum", 0);
        $session->set("sum", ++$tmp);
        $session->set("ends_at", $now);
    } else {
        $this->logger->info("[OTHER]");
        $tmp = $session->get("sum", 0);
        $session->set("sum", ++$tmp);
    }

    // Checking:
    $s = $session->get("starts_at", null);
    $e = $session->get("ends_at", null);
    $this->logger->info("ON SESSION [starts_at] -> " . ((isset($s) ? $s->format('r') : '--')));
    $this->logger->info("ON SESSION [ends_at] -> " . ((isset($e) ? $e->format('r') : '--')));
    $this->logger->info("ON SESSION [sum] -> " . $session->get("sum", null));

    return new JsonResponse(array("code" => "ok"), Codes::HTTP_OK);
}

然后用其他方法:

$startsAt = $session->get("starts_at", null);
$endsAt = $session->get("ends_at", null);
$this->logger->info("-- [starts_at] -> " . ((isset($startsAt) ? $startsAt->format('r') : '--')));
$this->logger->info("-- [ends_at] -> " . ((isset($endsAt) ? $endsAt->format('r') : '--')));

在日志中我得到了这个:

[2016-11-28 10:20:40] app.INFO: [START] [] []
[2016-11-28 10:20:40] app.INFO: ON SESSION [starts_at] -> Mon, 28 Nov 2016 10:20:40 +0100 [] []
[2016-11-28 10:20:40] app.INFO: ON SESSION [ends_at] -> -- [] []
[2016-11-28 10:20:40] app.INFO: ON SESSION [sum] -> 0 [] []
[2016-11-28 10:20:41] app.INFO: [OTHER] [] []
[2016-11-28 10:20:41] app.INFO: ON SESSION [starts_at] -> Mon, 28 Nov 2016 10:20:40 +0100 [] []
[2016-11-28 10:20:41] app.INFO: ON SESSION [ends_at] -> -- [] []
[2016-11-28 10:20:41] app.INFO: ON SESSION [sum] -> 1 [] []
[2016-11-28 10:20:42] app.INFO: [OTHER] [] []
[2016-11-28 10:20:42] app.INFO: ON SESSION [starts_at] -> Mon, 28 Nov 2016 10:20:40 +0100 [] []
[2016-11-28 10:20:42] app.INFO: ON SESSION [ends_at] -> -- [] []
[2016-11-28 10:20:42] app.INFO: ON SESSION [sum] -> 2 [] []
[2016-11-28 10:20:43] app.INFO: [OTHER] [] []
[2016-11-28 10:20:43] app.INFO: ON SESSION [starts_at] -> Mon, 28 Nov 2016 10:20:40 +0100 [] []
[2016-11-28 10:20:43] app.INFO: ON SESSION [ends_at] -> -- [] []
[2016-11-28 10:20:43] app.INFO: ON SESSION [sum] -> 3 [] []
[2016-11-28 10:20:44] app.INFO: [OTHER] [] []
[2016-11-28 10:20:44] app.INFO: ON SESSION [starts_at] -> Mon, 28 Nov 2016 10:20:40 +0100 [] []
[2016-11-28 10:20:44] app.INFO: ON SESSION [ends_at] -> -- [] []
[2016-11-28 10:20:44] app.INFO: ON SESSION [sum] -> 4 [] []
[2016-11-28 10:20:45] app.INFO: [OTHER] [] []
[2016-11-28 10:20:45] app.INFO: ON SESSION [starts_at] -> Mon, 28 Nov 2016 10:20:40 +0100 [] []
[2016-11-28 10:20:45] app.INFO: ON SESSION [ends_at] -> -- [] []
[2016-11-28 10:20:45] app.INFO: ON SESSION [sum] -> 5 [] []
[2016-11-28 10:20:46] app.INFO: [END] [] []
[2016-11-28 10:20:46] app.INFO: ON SESSION [starts_at] -> Mon, 28 Nov 2016 10:20:40 +0100 [] []
[2016-11-28 10:20:46] app.INFO: ON SESSION [ends_at] -> Mon, 28 Nov 2016 10:20:46 +0100 [] []
[2016-11-28 10:20:46] app.INFO: ON SESSION [sum] -> 5 [] []
[2016-11-28 10:20:52] app.INFO: -- [starts_at] -> Mon, 28 Nov 2016 10:20:40 +0100 [] []
[2016-11-28 10:20:52] app.INFO: -- [ends_at] -> -- [] []

最后, ends_at var(请求后六秒)未定义。但现在是执行,有时是 starts_at var是未定义的那个。

我输了。有人可以解释一下吗?

修改以添加更多信息:

经过一些测试后,我发现当设置了ends_at时,与 controlAction 同时获取了一个GET请求,并且延迟了第二个请求,错误似乎已经解决了。但是在该请求的控制器中没有使用会话对象......

编辑2:更多信息

如果在此GET请求中,在上次调用 controlAction 的同时发生了 $ session 并且"保存它"它似乎也解决了错误:

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

    [...]
}

1 个答案:

答案 0 :(得分:0)

正如我所说,当设置 ends_at 时,我看到GET请求与 controlAction 同时被提取。

如果在此GET请求中同时发生$session并“保存”,则似乎可以解决错误。

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

    [...]
}

根据文档$session->save() "force the session to be saved and closed"