I'm using 2.7 from Symphony.
Can I get arguments from the Symfony controller to the service container?
$hoge='any argument';
// I want to pass $hoge to the second argument of service
$service=$this->get('service');
I would like to pass any arguments to the constructor of the service.
service:
arguments —Class 1
arguments —Any argument
Well, I don't really understand what kind of use case it is, so I'm sorry if I said something wrong.
Perhaps if the controller wants to give you an argument, the value is determined only when the controller runs.
However, when an object is generated from service.yml is determined by when the container is built.
That is, it is decided before the controller runs.
Therefore, if you want to pass a value from the controller, you must provide the setter to the service side.
<?php
class HogeService
{
private$hoge;
public function setHoge($hoge)
{
$this->hoge=$hoge;
}
}
However, this method is not very recommended.
I think it's still possible to pass it as an argument for the method to be executed
$hoge='any argument';
// I want to pass $hoge to the second argument of service
$service=$this->get('service');
$service->execute($hoge);
It is very painful to force Symfony2 to implement when it is difficult to implement due to some constraints.
However, in most cases, the design is bad.
It might be a good idea to review the design and requirements once.
© 2024 OneMinuteCode. All rights reserved.