How to access an application parameters from a service?

后端 未结 9 779
梦如初夏
梦如初夏 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:38

    With Symfony 4.1 the solution is quite simple.

    Here is a snippet from the original post:

    // src/Service/MessageGenerator.php
    // ...
    
    use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
    
    class MessageGenerator
    {
        private $params;
    
        public function __construct(ParameterBagInterface $params)
        {
            $this->params = $params;
        }
    
        public function someMethod()
        {
            $parameterValue = $this->params->get('parameter_name');
            // ...
        }
    }
    

    Link to the original post: https://symfony.com/blog/new-in-symfony-4-1-getting-container-parameters-as-a-service

提交回复
热议问题