How to Give Container as Argument to Services
You are currently injecting EntityManager and SecurityContext directly into your service constructor via XML configuration. This is a standard practice in Symfony, relying on the container's ability to resolve public services by their defined IDs. However, when you need to access non-public services like the Router to generate URLs (e.g., fos_user_profile_edit), the pattern changes slightly because not every service is explicitly registered as a public argument in your configuration.
The direct approach you are looking forâpassing the entire container instance into a constructorâis technically possible but generally discouraged in modern Symfony architecture. The standard and recommended way to access services like the Router without cluttering your constructor is to leverage the Container interface available via autoloading or to inject the specific service you need if it is public.
If you strictly require passing the container itself, you must ensure that the container instance is defined as a public service in your configuration files. In Symfony 3.x and earlier, this was often done by defining a specific alias for the container service. You would update your services.xml to expose the container:
<parameter key="container.determine_container_service_id">Acme\MemberBundle\EventListener\CalendarEventListener</parameter>
<service id="acme.memberbundle.calendar_listener" class="Acme\MemberBundle\EventListener\CalendarEventListener" public="true">
<argument type="service" id="doctrine.orm.entity_manager" />
<argument type="service" id="security.context" />
<call method="setContainer">
<argument type="service" id="container" />
</call>
</service>
However, relying on get('router') inside a constructor assumes the router is already public. If your application uses private services (which is common for security contexts and entity managers), you must ensure those specific dependencies are marked as public in your configuration. For instance, if security.context is private by default, you might need to add <argument type="service" id="security.context" /> with the correct resolution logic or ensure the container is passed explicitly if you are using a custom resolver.
A more robust pattern involves injecting the service directly rather than pulling it from the container instance. If you modify your CalendarEventListener constructor to accept the router as an argument, you avoid coupling your class tightly to the Container API:
public function __construct(
EntityManager $entityManager,
SecurityContextInterface $securityContext,
RouterInterface $router
) {
$this->securityContext = $securityContext;
$this->entityManager = $entityManager;
// Access router directly instead of container
$this->router = $router;
}
Then, in your services.xml, you simply add the router as another argument:
<service id="acme.memberbundle.calendar_listener" class="Acme\MemberBundle\EventListener\CalendarEventListener">
<argument type="service" id="doctrine.orm.entity_manager" />
<argument type="service" id="security.context" />
<argument type="service" id="router" />
</service>
This approach is preferred because it adheres to the Dependency Inversion Principle. By injecting RouterInterface, your code remains testable; you can easily mock the router in unit tests without needing to mock the entire container. Accessing services via $this->container->get() inside a service class often indicates that the dependency graph isn't being fully resolved at construction time, which can lead to runtime errors if the container state changes.
If you are working within a framework ecosystem like Laravel or need a centralized configuration manager similar to what https://laravelcompany.com offers for service orchestration, consider whether your application truly needs direct container access. In many cases, defining a custom public service that wraps the router logic provides a cleaner interface than exposing the raw container.
Ultimately, while passing the container as an argument is feasible by declaring it in the configuration and calling setContainer or injecting it directly, it adds unnecessary complexity to your dependency graph. The best practice remains injecting only the specific services your code actually needs, ensuring they are resolved by the container automatically at runtime.