Symfony how to get container in my service

Symfony how to get container in my service

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Why Direct Container Access in Facades Is a Code Smell

Attempting to resolve dependencies inside a facade by directly accessing the service container—whether via $this->container or Container::getInstance()—signals a fundamental misunderstanding of how dependency injection works and why it exists. While your goal is clear: you want to create a facade for a complex service that controllers can use without knowing its internal wiring—you are proposing to bypass the Inversion of Control (IoC) pattern you explicitly stated you don't need in this specific instance, yet still rely on the container's magic to resolve those dependencies at runtime.

In Symfony, services registered in your config/services.yaml or services.php are not just random objects sitting in memory waiting to be retrieved; they are part of a strict graph managed by the Container component. When you write code like $this->container->get('service_from_my_domain'), you are coupling your class implementation directly to the container's lifecycle and configuration. This creates several tangible problems that will haunt you as your application scales.

The Problem with Manual Resolution

Your specific request asks for a way to avoid constructor injection or setter injection, preferring to pull dependencies out of the bag manually within the facade. Let's look at what happens when you try Container::getInstance()->get('my_dependency_service') inside your facade.

First, this approach destroys the benefits of lazy initialization. The container builds the dependency graph only when a service is requested. If your facade instantiates a heavy service or triggers a chain of dependencies manually without defining how those dependencies should be scoped (singleton vs. prototype), you risk memory leaks or inconsistent state across requests.

Second, and more critically, it couples your business logic to the framework's internal implementation details. By calling get() directly, you are forcing the facade to know exactly which service IDs exist in the container. If a developer renames a service from my_dependency_service to my_dependency_v2 in the config file, your facade will crash at runtime with an unhelpful "Service not found" exception. Constructor injection handles this gracefully by failing fast at compile time or load time, allowing static analysis tools and IDEs to catch missing dependencies before you ever run a single test.

The Correct Pattern: Delegate, Don't Resolve

If your intention is to create a facade that simplifies the usage of a large service without exposing its complexity to controllers, the solution isn't to reach into the container; it's to let the container do the wiring for you via constructor injection.

You can achieve a "facade-like" experience by injecting the main service into the facade itself, rather than trying to resolve its dependencies on the fly. The facade becomes a thin wrapper that delegates calls to the underlying domain object.

// services.yaml
services:
    app.facade.my_service_wrapper:
        class: App\Facade\MyServiceWrapper
        arguments:
            $myService: 'service_from_my_domain'
        
    # Your controller can simply use the facade
    App\Controller\MyController:
        arguments:
            $wrapper: 'app.facade.my_service_wrapper'

Inside MyServiceWrapper, you receive $myService in the constructor. Now, inside your facade methods, you call $this->myService->doSomething(). The controller sees a simple interface defined by your facade, but the heavy lifting and dependency resolution remain strictly encapsulated within the service layer where they belong.

Why This Approach Wins

This pattern preserves the decoupling that makes Symfony applications maintainable. It allows you to refactor the internal structure of service_from_my_domain without touching any controller code, provided your facade's public API remains consistent.

If you truly believe that Inversion of Control is unnecessary for this specific service, that is a valid architectural stance. However, abandoning IoC in favor of manual container resolution is not a neutral alternative; it is a regression. Manual resolution reintroduces tight coupling to the framework and makes testing significantly harder, as mocks become difficult to wire up when you are hard-coding service IDs.

There is no "magic" command or undocumented parameter that allows you to safely retrieve dependencies from the container inside a facade without accepting the risks of direct coupling. The trade-off is clear: you gain a tiny bit of configurational flexibility at the cost of architectural integrity, testability, and long-term maintainability. Sticking to constructor injection for the facade itself ensures that your code remains predictable, self-documenting, and resilient to changes in your dependency graph.

Our network