Search code examples
symfonybundlesymfony4

Define services parameters inside a sf4 bundle


I'm trying to configure a service inside a bundle. This service needs some parameters from the.env file but I can't declare these parameters in the service. Here's my code:

src/Dfc2/WsBundle/Services/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 constructor:

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();

}

And this is the error message I get.

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.

I don't understand what's wrong. Can you help me?


Solution

  • Thanks to Mathieu Dormeval and Cerad, I changed my service definition like this and now it works:

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