【问题标题】:How to inject service as argument in Symfony?如何在 Symfony 中注入服务作为参数?
【发布时间】:2019-06-28 21:19:50
【问题描述】:

在 Symfony 中覆盖父服务参数并向子服务参数添加更多参数的正确方法是什么?根据文档,我需要使用“index_N”。但是如何为子服务添加更多参数呢?例如:

config/services.yaml

服务: # ...

App\Repository\DoctrineUserRepository:
    parent: App\Repository\BaseDoctrineRepository

    # overrides the public setting of the parent service
    public: false

    # appends the '@app.username_checker' argument to the parent
    # argument list
    arguments: ['@app.username_checker']

App\Repository\DoctrinePostRepository:
    parent: App\Repository\BaseDoctrineRepository

    # overrides the first argument (using the special index_N key)
    arguments:
        index_0: '@doctrine.custom_entity_manager'
        // put here more arguments for the child service

【问题讨论】:

    标签: php symfony service dependency-injection


    【解决方案1】:

    未经测试,但您是否尝试过使用“命名参数”,如下所示:

    # overrides the first argument (using the special index_N key)
    # and set named argument $usernameChecker
    arguments:
        index_0: '@doctrine.custom_entity_manager'
        $usernameChecker: '@app.username_checker'
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-23
    • 1970-01-01
    • 1970-01-01
    • 2017-06-03
    • 1970-01-01
    • 2012-08-01
    • 2018-01-22
    相关资源
    最近更新 更多