我使用 symfony 2.5,并且我有下一个独白服务。
<?php
namespace Megogo\CoreBundle;
use Symfony\Component\HttpFoundation\Session\Session;
class SessionRequestProcessor
{
/**
* @var \Symfony\Component\HttpFoundation\Session\Session
*/
protected $session;
/**
* @var
*/
private $token;
public function __construct( Session $session)
{
$this->session = $session;
}
public function processRecord(array $record)
{
if (null === $this->token) {
try {
$this->token = $this->session->getId();
} catch (\RuntimeException $e) {
$this->token = '????????';
}
}
$record['extra']['token'] = $this->token;
return $record;
}
}
服务.yml
services:
monolog.formatter.session_request:
class: Monolog\Formatter\LineFormatter
arguments:
- "[%%datetime%%] [%%extra.token%%] %%channel%%.%%level_name%%: %%message%%\n"
monolog.processor.session_request:
class: Megogo\CoreBundle\SessionRequestProcessor
arguments: ["@session"]
tags:
- { name: monolog.processor, method: processRecord }
我从官方文档中获取示例。添加会话/请求令牌
我有问题,$this->session->getId()
返回空字符串。如果我添加 $session->start();
所有作品。我可以获得会话ID。但这很奇怪,因为在我的其他服务中,如果没有这种解决方法,所有服务都可以正常工作。当我做app/console cache:clear
我有错误Failed to start the session: already started by PHP.