I have symfony project and inside this project, I have big own service which is huge and complicated with own dependencies etc...
And I wanna create facade for this service with purpose to use my service in controllers
like:
$myService = $this->container->get('service_from_my_domain');
My question - is how inside my facade I can get access to container
to service's dependencies. I know only 1 way - is to inject dependency into service in yaml config.
But is there another way to do it? Like:
$dependency = Container::getInstance()->get('my_dependency_service');
I've found this answer but using global variable feels like back in time...
PS: I don't want to inject dependency through yaml config (not constructor injection nor setter injection) because I don't need IoC (inversion-of-control) here.
can you do like this
services:
kernel.listener.acme_listener:
class: Acme\AcmeBundle\EventListener\AcmeListener
arguments:
- @service_container
tags:
- { name: kernel.event_listener, event: kernel.controller, method: onKernelController }
your Listener
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
class AcmeListener
{
/**
* @var Container
*/
private $container;
/**
* Constructor
*
* @param Container $container
*/
public function __construct(Container $container)
{
$this->container = $container;
}
public function onKernelController(FilterControllerEvent $event)
{
$this->container->...
}
}