[英]symfony2 empty session id in service
I use symfony 2.5, and I have next service for monolog. 我使用symfony 2.5,我有下一个monolog服务。
<?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;
}
}
service.yml service.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 }
I get example from official documentation. 我从官方文档中得到了例子。 Adding a Session/Request Token
添加会话/请求令牌
I have problem, that $this->session->getId()
return empty string. 我有问题,
$this->session->getId()
返回空字符串。 If I add $session->start();
如果我添加
$session->start();
all works. 一切都有效。 I can get session id.
我可以获得会话ID。 But this is weird, because in other my services all works without this workaround.
但这很奇怪,因为在其他我的服务中,所有工作都没有这种解决方法。 And when I make
app/console cache:clear
I have error Failed to start the session: already started by PHP.
当我创建
app/console cache:clear
我有错误Failed to start the session: already started by PHP.
I've had a similar issue. 我有类似的问题。
I've solved it by adding in a controller 我通过添加一个控制器来解决它
$this->get('session')->start();
In your case you could simply add following line before you try to get session ID 在您的情况下,您可以在尝试获取会话ID之前添加以下行
$this->session->start();
Don't worry about calling "start()" when session is already started - it will check and simply skip (re)starting it. 当会话已经启动时,不要担心调用“start()” - 它将检查并简单地跳过(重新)启动它。
Current (2.6) version of Symfony does not have config property which would force session auto_start Symfony的当前(2.6)版本没有配置属性 ,这将强制会话auto_start
Official docs says: 官方文档说:
Symfony sessions are incompatible with php.ini directive session.auto_start = 1 This directive should be turned off in php.ini, in the webserver directives or in .htaccess.
Symfony会话与php.ini指令session.auto_start = 1不兼容。该指令应在php.ini,webserver指令或.htaccess中关闭。
Maybe try this? 也许试试这个?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.