首页 > 解决方案 > How to override service arguments during it's call

问题描述

I have a class to generate CSV file by accepting the data it requires. I have declared this class as symfony service like,

<service id="admin.csv_response" class="AdminBundle\Controller\CsvResponse" public="true"> <argument key="$data">NULL</argument> <argument key="$fileName">export.csv</argument> </service>

I need to call this service from controller like,

$this->get('admin.csv_response');

How can I pass the values of $data and $fileName during this call by passing these as parameters so that it overrides the already given values?

I tried the following line, but it doesn't work.

$this->get('admin.csv_response', ['sample data', 'xxx.csv']);

Thanks in advance!!

标签: phpsymfony

解决方案


只需在您的服务中添加 2 个 setter 方法,而不是通过构造函数注入它们:

setData(string $data){...};
setFileName(string $fileName){...};

然后打电话给他们:

$this->get('admin.csv_response')->setData(...)->setFileName(...);

推荐阅读