在每个请求上实例化的Symfony服务

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

我有一个名为TennantContext的服务,该服务处理所有与租户相关的功能,包括跟踪用户当前处于活动状态的当前租户。

我的印象是,一旦实例化该服务就不再实例化?我正确吗?

我有以下问题:

我正在将TenantContect服务注入到多个控制器和事件触发器中,但是每次注入时,它都会再次被实例化,例如构造函数将再次运行,并且服务中的所有先前值均已重置:

TenantContext:

class TenantContext
{
    private $tenant;
    private $isInitialized = false;
    ...
    ...
    public function __construct(
        Security $security,
        LoggerInterface $logger,
        UserService $userService,
        EntityManagerInterface $entityManager
    )
    {
        $this->logger = $logger;
        $this->logger->info("YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY: Tenant instantiated");
        ...
        ...
    }

这首先用我的LoginEventListener实例化,成功登录后将在上下文中设置租户:

class LoginListener
{
    /**
     * 
     * @var EntityManagerInterface
     */
    private $em;

    /**
     * 
     * @var TenantContext
     */
    private $tenantContext;

    /**
     * 
     * @var LoggerInterface
     */
    private $logger;

    public function __construct(
        EntityManagerInterface $em,
        LoggerInterface $logger,
        TenantContext $tenantContext
        )
    {
        $this->em = $em;
        $this->logger = $logger;
        $this->tenantContext = $tenantContext;
    }

    public function onSecurityInteractiveLogin(InteractiveLoginEvent $event)
    {
        $this->logger->info("------------------------------------- onSecurityInteractiveLogin");
        // Get the User entity.
        /**
         * 
         * @var User $user
         */
        $user = $event->getAuthenticationToken()->getUser();
        $this->tenantContext->initialize($user->getTenant());
    }
}

这很好用,但是当我导航到另一个页面时(例如使用Twig中的路线:{{path('dashboard')}}]

日志:

Jan 10 14:38:11 |INFO | PHP    Matched route "dashboard".
Jan 10 14:38:11 |DEBUG| PHP    Read existing security token from the session.
Jan 10 14:38:11 |DEBUG| PHP    SELECT t0.id AS id_1, t0.first_name AS first_name_2, t0.last_name AS last_name_3, t0.email AS email_4, t0.password AS password_5, t0.status AS status_6, t0.guid AS guid_7, t0.created_date AS created_date_8, t0.modif
0, t0.user_type AS user_type_11, t0.roles AS roles_12, t0.created_by_id AS created_by_id_13, t0.modified_by_id AS modified_by_id_14 FROM user t0 WHERE t0.id = ?
Jan 10 14:38:11 |DEBUG| PHP    User was reloaded from a user provider.
Jan 10 14:38:11 |DEBUG| PHP    Checking for guard authentication credentials.
Jan 10 14:38:11 |DEBUG| PHP    Checking support on guard authenticator.
Jan 10 14:38:11 |DEBUG| PHP    Guard authenticator does not support the request.
Jan 10 14:38:11 |DEBUG| PHP    Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\DebugHandlersListener::configure".
Jan 10 14:38:11 |DEBUG| PHP    Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\ValidateRequestListener::onKernelRequest".
Jan 10 14:38:11 |DEBUG| PHP    Notified event "kernel.request" to listener "Nelmio\CorsBundle\EventListener\CorsListener::onKernelRequest".
Jan 10 14:38:11 |DEBUG| PHP    Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\SessionListener::onKernelRequest".
Jan 10 14:38:11 |DEBUG| PHP    Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\LocaleListener::setDefaultLocale".
Jan 10 14:38:11 |DEBUG| PHP    Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\RouterListener::onKernelRequest".
Jan 10 14:38:11 |DEBUG| PHP    Notified event "kernel.request" to listener "Symfony\Bundle\FrameworkBundle\EventListener\ResolveControllerNameSubscriber::onKernelRequest".
Jan 10 14:38:11 |DEBUG| PHP    Notified event "kernel.request" to listener "ApiPlatform\Core\Filter\QueryParameterValidateListener::onKernelRequest".
Jan 10 14:38:11 |DEBUG| PHP    Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\LocaleListener::onKernelRequest".
Jan 10 14:38:12 |DEBUG| PHP    Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\LocaleAwareListener::onKernelRequest".
Jan 10 14:38:12 |DEBUG| PHP    Notified event "kernel.request" to listener "Symfony\Bundle\SecurityBundle\Debug\TraceableFirewallListener::configureLogoutUrlGenerator".
Jan 10 14:38:12 |DEBUG| PHP    Notified event "kernel.request" to listener "Symfony\Bundle\SecurityBundle\Debug\TraceableFirewallListener::onKernelRequest".
Jan 10 14:38:12 |DEBUG| PHP    Notified event "kernel.request" to listener "ApiPlatform\Core\EventListener\AddFormatListener::onKernelRequest".
Jan 10 14:38:12 |DEBUG| PHP    Notified event "kernel.request" to listener "ApiPlatform\Core\EventListener\ReadListener::onKernelRequest".
Jan 10 14:38:12 |DEBUG| PHP    Notified event "kernel.request" to listener "ApiPlatform\Core\Security\EventListener\DenyAccessListener::onSecurity".
Jan 10 14:38:12 |DEBUG| PHP    Notified event "kernel.request" to listener "ApiPlatform\Core\EventListener\DeserializeListener::onKernelRequest".
Jan 10 14:38:12 |DEBUG| PHP    Notified event "kernel.request" to listener "ApiPlatform\Core\Security\EventListener\DenyAccessListener::onSecurityPostDenormalize".
Jan 10 14:38:12 |DEBUG| PHP    Notified event "kernel.request" to listener "ApiPlatform\Core\Bridge\Symfony\Bundle\EventListener\SwaggerUiListener::onKernelRequest".
Jan 10 14:38:12 |DEBUG| PHP    Notified event "kernel.request" to listener "Knp\Bundle\PaginatorBundle\Subscriber\SlidingPaginationSubscriber::onKernelRequest".
Jan 10 14:38:12 |INFO | PHP    User Deprecated: The "templating.locator" service is deprecated since Symfony 4.3 and will be removed in 5.0.
Jan 10 14:38:12 |INFO | PHP    User Deprecated: The Symfony\Bundle\FrameworkBundle\Templating\Loader\TemplateLocator class is deprecated since version 4.3 and will be removed in 5.0; use Twig instead.
Jan 10 14:38:12 |INFO | PHP    User Deprecated: The "templating.name_parser" service is deprecated since Symfony 4.3 and will be removed in 5.0.
Jan 10 14:38:12 |INFO | PHP    User Deprecated: The Symfony\Bundle\FrameworkBundle\Templating\TemplateNameParser class is deprecated since version 4.3 and will be removed in 5.0; use Twig instead.
Jan 10 14:38:12 |DEBUG| PHP    Notified event "kernel.controller" to listener "Symfony\Bundle\FrameworkBundle\DataCollector\RouterDataCollector::onKernelController".
Jan 10 14:38:12 |DEBUG| PHP    Notified event "kernel.controller" to listener "Symfony\Component\HttpKernel\DataCollector\RequestDataCollector::onKernelController".
Jan 10 14:38:12 |DEBUG| PHP    Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\ControllerListener::onKernelController".
Jan 10 14:38:12 |DEBUG| PHP    Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\ParamConverterListener::onKernelController".
Jan 10 14:38:12 |DEBUG| PHP    Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\HttpCacheListener::onKernelController".
Jan 10 14:38:12 |DEBUG| PHP    Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\TemplateListener::onKernelController".
Jan 10 14:38:12 |INFO | PHP    YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY: Tenant instantiated
Jan 10 14:38:12 |DEBUG| PHP    Notified event "debug.security.authorization.vote" to listener "Symfony\Bundle\SecurityBundle\EventListener\VoteListener::onVoterVote".
Jan 10 14:38:12 |DEBUG| PHP    Notified event "kernel.controller_arguments" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\SecurityListener::onKernelControllerArguments".
Jan 10 14:38:12 |DEBUG| PHP    Notified event "kernel.controller_arguments" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\IsGrantedListener::onKernelControllerArguments".
Jan 10 14:38:12 |INFO | PHP    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX: Reinitializing tenant
Jan 10 14:38:12 |DEBUG| PHP    SELECT t0.id AS id_1, t0.default_org AS default_org_2, t0.user_id AS user_id_3, t0.organization_id AS organization_id_4 FROM user_organization t0 WHERE t0.user_id = ?
Jan 10 14:38:12 |INFO | PHP    ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ: Tenant initialized with id: 5
Jan 10 14:38:12 |INFO | PHP    User Deprecated: Using the "templating" service is deprecated since version 4.3 and will be removed in 5.0; use Twig instead.
Jan 10 14:38:12 |INFO | PHP    User Deprecated: The Symfony\Bridge\Twig\TwigEngine class is deprecated since version 4.3 and will be removed in 5.0; use \Twig\Environment instead.
Jan 10 14:38:12 |INFO | PHP    User Deprecated: The Symfony\Bundle\FrameworkBundle\Templating\EngineInterface interface is deprecated since version 4.3 and will be removed in 5.0; use Twig instead.
Jan 10 14:38:12 |INFO | PHP    User Deprecated: The Symfony\Bundle\TwigBundle\TwigEngine class is deprecated since version 4.3 and will be removed in 5.0; use \Twig\Environment instead.

我可以从构造函数再次调用的日志中看到,对于每个请求都会发生这种情况。

如何使TenantContext成为整个会话的一个实例?

提前感谢。

编辑:看来这也发生在其他服务上,我在我拥有的另一个服务上添加了一些日志记录,并且执行相同的操作。就我所有的服务不包含任何数据而只是函数而言,我还没有注意到这一点。

symfony service controller
1个回答
0
投票

根据评论,我现在意识到我对服务的理解是不正确的,并且在每次请求时都会实例化它们。

因此,为了解决我的问题,我将hte ID存储在会话中,并在需要时使用会话中的ID检索数据。

© www.soinside.com 2019 - 2024. All rights reserved.