Symfony 5 symfony/dependency-injection container does not handle service by name

710 Views Asked by At

In the config file services.yaml I create several services with names

cart.storage.session:
    class: App\Cart\Storage\SessionStorage
    arguments:
        - '%cart.storage.session_key%'

cart.calculator:
    class: App\Cart\Cost\SimpleCost

cart:
    class: App\Cart\Cart
    arguments:
        - '@cart.storage.session'
        - '@cart.calculator'

I am using these services in my controller

$cart = $this->container->get('cart');

But for some reason, with such a call to the service, I get an exception.

Service "cart" not found: even though it exists in the app's container, the container inside "App\Controller\CartController" is a smaller service locator that only knows about the "doctrine", "http_kernel", "parameter_bag", "request_stack", "router", "security.authorization_checker", "security.csrf.token_manager", "security.token_storage" and "session" services. Try using dependency injection instead.

The bin/console debug:container command shows that the container contains my services, but for some reason they are not named

-------------------------------------------------------------
 Service ID                     Class name
--------------------------------------------------------------

App\Cart\Cart                    App\Cart\Cart
App\Cart\CartItem                App\Cart\CartItem
App\Cart\Cost\BigCost            App\Cart\Cost\BigCost
App\Cart\Cost\BirthdayCost       App\Cart\Cost\BirthdayCost
App\Cart\Cost\DummyCost          App\Cart\Cost\DummyCost
App\Cart\Cost\FourCost           App\Cart\Cost\FourCost
App\Cart\Cost\FridayCost         App\Cart\Cost\FridayCost
App\Cart\Cost\MinCost            App\Cart\Cost\MinCost
App\Cart\Cost\NewYearCost        App\Cart\Cost\NewYearCost
App\Cart\Cost\SimpleCost         App\Cart\Cost\SimpleCost
App\Cart\Storage\DBStorage       App\Cart\Storage\DBStorage
App\Cart\Storage\HybridStorage   App\Cart\Storage\HybridStorage
App\Cart\Storage\MemoryStorage   App\Cart\Storage\MemoryStorage
App\Cart\Storage\SessionStorage  App\Cart\Storage\SessionStorage
App\Controller\CartController    App\Controller\CartController

Then I changed my services.yaml as follows

cart.storage.session:
    class: App\Cart\Storage\SessionStorage
    arguments:
        - '%cart.storage.session_key%'
App\Cart\Storage\SessionStorage: '@cart.storage.session'

cart.calculator:
    class: App\Cart\Cost\SimpleCost
App\Cart\Cost\SimpleCost: '@cart.calculator'

cart:
    class: App\Cart\Cart
    arguments:
        - '@cart.storage.session'
        - '@cart.calculator'
App\Cart\Cart: '@cart'

After that my services got names and the bin/console debug:container command began to display the following

-------------------------------------------------------------
 Service ID                     Class name
--------------------------------------------------------------

App\Cart\Cart                    alias for "cart"
App\Cart\CartItem                App\Cart\CartItem
App\Cart\Cost\BigCost            App\Cart\Cost\BigCost
App\Cart\Cost\BirthdayCost       App\Cart\Cost\BirthdayCost
App\Cart\Cost\DummyCost          App\Cart\Cost\DummyCost
App\Cart\Cost\FourCost           App\Cart\Cost\FourCost
App\Cart\Cost\FridayCost         App\Cart\Cost\FridayCost
App\Cart\Cost\MinCost            App\Cart\Cost\MinCost
App\Cart\Cost\NewYearCost        App\Cart\Cost\NewYearCost
App\Cart\Cost\SimpleCost         alias for "cart.calculator"
App\Cart\Storage\DBStorage       App\Cart\Storage\DBStorage
App\Cart\Storage\HybridStorage   App\Cart\Storage\HybridStorage
App\Cart\Storage\MemoryStorage   App\Cart\Storage\MemoryStorage
App\Cart\Storage\SessionStorage  alias for "cart.storage.session"
App\Controller\CartController    App\Controller\CartController

But it still didn't fix the situation because I still get the same exception as before.

Service "cart" not found: even though it exists in the app's container, the container inside

0

There are 0 best solutions below