在我的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标头设置为其他任何内容。
有人知道怎么了吗?