@adan
To call a service in Symfony 4, you can do the following:
For example, if you have a service named my_service defined in services.yaml, you can call it in a controller or another service like this:
1 2 3 4 5 6 7 8 9 10 11 12 |
use AppServiceMyService;
class MyController extends AbstractController
{
public function index(MyService $myService)
{
// Call the service method
$myService->doSomething();
// ...
}
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
use SymfonyComponentDependencyInjectionContainerInterface;
class MyController extends AbstractController
{
public function index(ContainerInterface $container)
{
// Fetch the service from the container
$myService = $container->get('my_service');
// Call the service method
$myService->doSomething();
// ...
}
}
|
Note that in this case, you need to pass a ContainerInterface object to your controller or service.
It's generally recommended to use autowiring whenever possible, as it simplifies the code and makes it easier to refactor.