在sf4包中定义服务参数

时间:2018-03-29 11:47:40

标签: symfony bundle symfony4

我尝试在捆绑包内配置服务。此服务需要来自.env文件的一些参数,但我无法在服务中声明这些参数。这是我的代码:

的src / Dfc2 / WsBundle /服务/ WsManager / WsManager.yaml

parameters:
    wsAdminUser: '%env(WSADMIN_USER)%'
    wsAdminPassword: '%env(WSADMIN_PASSWORD)%'
services:
    _defaults:
        autowire: true
        autoconfigure: true
        public: false

    WsManager:
        public: true
        class: App\Dfc2\WsBundle\Services\WsManager\WsManager
        arguments:
            $env: '%kernel.environment%'
            $wsAdminUser: '%wsAdminUser%'
            $wsAdminPassword: '%wsAdminPassword%'

src / Dfc2 / WsBundle / Services / WsManager / WsManager.php构造函数:

public function __construct($env, RequestStack $requestStack, SessionInterface $session, string $wsAdminUser, string $wsAdminPassword)
{

    $this->environement = $env;
    $this->wsAdminUser = $wsAdminUser;
    $this->wsAdminPassword = $wsAdminPassword;
    $this->session = $session;
    $this->baseUrl = $requestStack->getCurrentRequest()->getBaseUrl() . WsParameters::URL_SUFFIX;
    $this->setBaseUrl();

}

这是我收到的错误消息。

Cannot autowire service "App\Dfc2\WsBundle\Services\WsManager\WsManager": argument "$wsAdminUser" of method "__construct()" must have a type-hint or be given a value explicitly.

我不明白什么是错的。你能救我吗?

1 个答案:

答案 0 :(得分:1)

感谢Mathieu Dormeval和Cerad, 我改变了我的服务定义,现在它可以工作了:

services:
    App\Dfc2\WsBundle\Services\WsManager\WsManager:
        autowire: false
        public: true
        arguments:
            $env: '%kernel.environment%'
            $requestStack: '@request_stack'
            $session: '@session'
            $wsAdminUser: '%wsAdminUser%'
            $wsAdminPassword: '%wsAdminPassword%'