I appeal to the knowledge of the community to resolve a doubt about the use of services, about whether it is feasible to use the functionalities of one service within another.
I have two services defined whose nomenclature is as follows:
GestorSolicitudController
GestorTurnosController
If in the GestorTurnoController , I try to call a function declared in GestorTurnosController as follows:
$servicio = $this->container->get('gestor_solicitudes');
$solicitud = $servicio->obtenerSolicitudActual($dni);
I get the error Call to a member function get() on null Provisionally, I solved it using dependency injection, declaring an attribute as follows:
class GestorTurnoController extends Controller
{
protected $entityManager;
//usamos injeccion de dependencias porque no puedo usar un servicio en otro
//to-do ver como resolverlo
protected $solicitudes;
public function __construct($entityManager)
{
$this->entityManager = $entityManager;
$this->solicitudes = new GestorSolicitudController($entityManager);
}
In this way, it works correctly. But I have the doubt if it is possible to use a service within another, if it is feasible, can you tell me how and if it is not, I will solve it with dependency injection. Thank you!
I solved the problem with a concept that is new to me: service injection
Googling I found the following references of symfony 2.x link link2 Where they explain the use of injection of a service within another.
In my case, modify the services.yml file as follows:
The argument @gestor_solicitudes was incorporated, which refers to the other service that I am interested in using.
In the controller, modify the constructor as follows:
Now in the methods that I need to access a function of the injected service, I just write:
I hope it works for you!