php - 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!!
解决方案
只需在您的服务中添加 2 个 setter 方法,而不是通过构造函数注入它们:
setData(string $data){...};
setFileName(string $fileName){...};
然后打电话给他们:
$this->get('admin.csv_response')->setData(...)->setFileName(...);
推荐阅读
- java - 使用 Spring Boot Actuator 记录所有请求和响应
- php - 如果未输入用户名/密码,则使页面无法访问。PHP
- rabbitmq - RabbitMQ 消息大小问题
- php - 在 Laravel 5.5 中测试授权策略时遇到问题
- ios - Swift Codable 不能只自定义所需的键
- python - 连接2个modbus slave pymodbus时出错
- neo4j - Neo4J LOAD CSV 内存问题
- c# - c#中字段验证的正则表达式
- hadoop - Hadoop - 如何在运行时运行另一个 mapreduce 作业?
- winapi - ShellExecute 执行后保持屏幕并等待字符