Symfony2:如何在侦听器中获取配置参数? [英] Symfony2: How to get a config parameter within a listener?
本文介绍了Symfony2:如何在侦听器中获取配置参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个监听器服务。
我想读取其中的一些配置参数。
I have a listener service. I want to read some config parameters inside it.
如何在侦听器类中访问服务容器?
How can I access the service container within the listener class?
推荐答案
您可以使用%your_param_name%
表示法将容器参数传递到服务中:
You can pass container parameters into your service by using the %your_param_name%
notation:
services:
kernel.listener.locale_listener:
class: My\BundleName\Listener\LocaleListener
tags:
- { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }
arguments: [ @router, @service_container, %your_param_name% ]
会在服务的构造方法中作为第三个参数显示(在本例中):
which will present itself as (in this example) the 3rd argument in your service's constructor method:
public function __construct($router, $container, $paramValue)
{
// ...
}
这篇关于Symfony2:如何在侦听器中获取配置参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文