为了账号安全,请及时绑定邮箱和手机立即绑定

Symfony 服务,tagged_locator 适用于 yaml 中的配置,但不适用于 php

Symfony 服务,tagged_locator 适用于 yaml 中的配置,但不适用于 php

PHP
慕田峪4524236 2023-10-21 17:22:21
我在服务配置中使用 tagged_locator 一切都可以使用 yaml 配置。但是当我在php中进行配置时,它不再起作用了。我的服务的参数没有填写(0个providedServices)配置Yamlservices:    # default configuration for services in *this* file    _defaults:        autowire: true      # Automatically injects dependencies in your services.        autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.    # makes classes in src/ available to be used as services    # this creates a service per class whose id is the fully-qualified class name    App\:        resource: '../src/'        exclude:            - '../src/DependencyInjection/'            - '../src/Entity/'            - '../src/Kernel.php'            - '../src/Tests/'    # controllers are imported separately to make sure services can be injected    # as action arguments even if you don't extend any base controller class    App\Controller\:        resource: '../src/Controller/'        tags: ['controller.service_arguments']    # add more service definitions when explicit configuration is needed    # please note that last definitions always *replace* previous ones    # Will tag automatically all service that implement the VoterInterface created    _instanceof:        App\Voter\CriterionInterface:            tags:                - 'app.post.voter.criterion'    App\Voter\PostVoter:        arguments:            - !tagged_locator 'app.post.voter.criterion'配置PHPreturn static function (ContainerConfigurator $containerConfigurator): void {    $services = $containerConfigurator->services();    $services->defaults()        ->autowire()        ->autoconfigure();    $services->load('App\\', __DIR__.'/../src/')        ->exclude(            [                __DIR__.'/../src/DependencyInjection/',                __DIR__.'/../src/Entity/',                __DIR__.'/../src/Kernel.php',                __DIR__.'/../src/Tests/',            ]        );
查看完整描述

1 回答

?
LEATH

TA贡献1936条经验 获得超6个赞

配置器是不可变的:您需要存储和使用调用defaults()和instanceof()的返回值:


    $services = $services->defaults()

        ->autowire()

        ->autoconfigure();


    $services = $services->instanceof(CriterionInterface::class)

        ->tag('app.post');


    $services->load('App\\', __DIR__.'/../src/')

        ->exclude(

            [

                __DIR__.'/../src/DependencyInjection/',

                __DIR__.'/../src/Entity/',

                __DIR__.'/../src/Kernel.php',

                __DIR__.'/../src/Tests/',

            ]

        );

Instanceof 条件必须在基于路径的服务发现之前配置(第一次调用 load())


查看完整回答
反对 回复 2023-10-21
  • 1 回答
  • 0 关注
  • 83 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信