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

我在服务配置中使用 tagged_locator 一切都可以使用 yaml 配置。但是当我在php中进行配置时,它不再起作用了。我的服务的参数没有填写(0个providedServices)


配置Yaml


services:

    # 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'


配置PHP


return 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/',

            ]

        );



慕田峪4524236
浏览 84回答 1
1回答

LEATH

配置器是不可变的:您需要存储和使用调用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())
打开App,查看更多内容
随时随地看视频慕课网APP