如何从symfony2的控制器中的parameters.yml读取?
我在app / config / parameters.yml中放了几个自定义variables。
parameters: api_pass: apipass api_user: apiuser 我需要从我的控制器访问这些,并试图获取它们
 $this->get('api_user'); 
从我的控制器文件中。 当我尝试这个,我得到这个错误消息:
 You have requested a non-existent service "api_user". 
什么是正确的方法来做到这一点?
在Symfony 2.6及更早版本中 ,为了在控制器中获取参数 – 您应该首先获取容器,然后获取所需的参数。
 $this->container->getParameter('api_user'); 
这个文档章节解释它。
 而控制器中的$this->get()方法将加载一个服务( doc ) 
在Symfony 2.7及更新版本中 ,要获取控制器中的参数,可以使用以下命令:
 $this->getParameter('api_user'); 
我给你一个swiftmailer的例子:
parameters.yml
 recipients: [email1, email2, email3] 
服务:
 your_service_name: class: your_namespace arguments: ["%recipients%"] 
服务的类别:
 protected $recipients; public function __construct($recipients) { $this->recipients = $recipients; } 
您可以使用:
 public function indexAction() { dump( $this->getParameter('api_user')); } 
欲了解更多信息,我build议你阅读文档:
http://symfony.com/doc/2.8/service_container/parameters.html
你也可以使用:
 $container->getParameter('api_user'); 
访问http://symfony.com/doc/current/service_container/parameters.html