How to access an application parameters from a service?

后端 未结 9 793
梦如初夏
梦如初夏 2020-12-23 10:49

From my controllers, I access the application parameters (those in /app/config) with

$this->container->getParameter(\'my_param\')
<         


        
9条回答
  •  一向
    一向 (楼主)
    2020-12-23 11:24

    You can pass parameters to your service in the same way as you inject other services, by specifying them in your service definition. For example, in YAML:

    services:
        my_service:
            class:  My\Bundle\Service\MyService
            arguments: [%my_param1%, %my_param2%]
    

    where the %my_param1% etc corresponds to a parameter named my_param1. Then your service class constructor could then be:

    public function __construct($myParam1, $myParam2)
    {
        // ...
    }
    

提交回复
热议问题