【发布时间】:2015-01-26 12:09:03
【问题描述】:
我有这个类,它声明为服务。当我get() 我的服务时,我运行一些方法,这个方法需要两个参数,我想让用户在config.yml 中配置。如何在此类中获取这些参数?在我的服务定义中可能存在某种方式来做到这一点?或者我需要从 ContainerAware 扩展我的课程(如果我是对的,这是不好的做法)?谢谢!
【问题讨论】:
标签: symfony service configuration containers
我有这个类,它声明为服务。当我get() 我的服务时,我运行一些方法,这个方法需要两个参数,我想让用户在config.yml 中配置。如何在此类中获取这些参数?在我的服务定义中可能存在某种方式来做到这一点?或者我需要从 ContainerAware 扩展我的课程(如果我是对的,这是不好的做法)?谢谢!
【问题讨论】:
标签: symfony service configuration containers
您可以使用%param_name% 语法将参数注入到您的服务中
services.yml
services:
your_service:
class: Acme\DemoBundle\YourClass
arguments: [@some.other.service, %my_parameter%]
parameters.yml
parameters:
my_parameter: my_value
【讨论】:
parameter的参数,它也可能是bundle添加的参数
您可以使用构造函数调用它们
acme.your_service:
class: Acme\DemoBundle\YourService
arguments: [%param1%]
在课堂上
class YourService {
protected $param1;
public function __construct($param1) {
$this->param1 = $param1;
}
}
【讨论】: