当Content-Type标头为application / json时,不会调用Symfony 4 onKernelRequest

时间:2019-03-05 15:16:08

标签: events symfony4 symfony-routing symfony-eventdispatcher

在我的Symfony 4应用中,我有一个onKernelRequest事件的侦听器:

class TenantListener
{
    use ConnectionAwareTrait;

    /**
     * @var ConnectionWrapper
     */
    private $connection;
    /**
     * @var DatabaseCredentialProviderInterface
     */
    private $dataBaseCredentialProvider;

    /**
     * TenantListener constructor.
     * @param ConnectionWrapper $connection
     * @param DatabaseCredentialProviderInterface $dataBaseCredentialProvider
     */
    public function __construct(ConnectionWrapper $connection, DatabaseCredentialProviderInterface $dataBaseCredentialProvider)
    {
        $this->connection = $connection;
        $this->dataBaseCredentialProvider = $dataBaseCredentialProvider;
    }

    public function onKernelRequest(GetResponseEvent $event)
    {
        if (! $event->isMasterRequest() || ! $this->multiTenantEnabled()) {
            return;
        }

        $database = $this->dataBaseCredentialProvider->getDatabase();
        $this->connection->forceSwitch($database->getHost(), $database->getName(), $database->getUsername(), $database->getPassword());
    }
}

使用service.yaml:

  App\MultiTenant\Listener\TenantListener:
    class: App\MultiTenant\Listener\TenantListener
    arguments:
      $connection: '@doctrine.dbal.central_connection'
    tags:
      - { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }

当Content-Type设置为application/vnd.api+json时,侦听器可以正常工作,大多数端点都是这种情况。但是有一个端点必须具有Content-Type application/json,对于该端点,在发送内容类型标头onKernelRequest时不会调用application/json方法,而在我发送时会调用将te标头设置为其他任何内容。

有人知道怎么了吗?

0 个答案:

没有答案