Symfony 4 - 尝试创建会话代理时出错

问题描述 投票:0回答:1

我按照https://symfony.com/doc/master/session/proxy_examples.html的说明,

我更新了我的framework.yaml

framework:
    secret: '%env(APP_SECRET)%'
    #default_locale: en
    #csrf_protection: ~
    #http_method_override: true

# uncomment this entire section to enable sessions
session:
    # With this config, PHP's native session handling is used
    handler_id: App\Session\CookieEncryptedSession

#esi: ~
#fragments: ~
php_errors:
    log: true

我也创建了自己的类:

<?php
namespace App\Session;

use Defuse\Crypto\Crypto;
use Defuse\Crypto\Key;
use Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy;

class CookieEncryptedSession extends SessionHandlerProxy
{
  private $key;

  public function __construct(\SessionHandlerInterface $handler, Key $key)
  {
    $this->key = $key;

    parent::__construct($handler);
  }

  public function read($id)
  {
    $data = parent::read($id);

    return Crypto::decrypt($data, $this->key);
  }

  public function write($id, $data)
  {
    $data = Crypto::encrypt($data, $this->key);

    return parent::write($id, $data);
  }
}

当我尝试使用控制台运行服务器时,我收到此错误:

In CheckCircularReferencesPass.php line 67:

  Circular reference detected for service "App\Session\CookieEncryptedSession  
  ", path: "App\Session\CookieEncryptedSession -> App\Session\CookieEncrypted  
  Session".     

知道哪里出错了?

谢谢

奥斯卡

php symfony session symfony4
1个回答
0
投票

自动装配试图将服务注入自身,因为服务实现了构造函数所需的接口。 CookieEncryptedSession通过以下方式实施SessionHandlerInterface

class SessionHandlerProxy extends AbstractProxy implements \SessionHandlerInterface

在服务中设置服务:CookieEncryptedSession手动,以便您可以选择所需的SessionHandlerInterface服务。

  • NativeSessionHandler
  • NativeFileSessionHandler
  • DbalSessionHandler
  • 等等
© www.soinside.com 2019 - 2024. All rights reserved.