Why do we use the Containerinterface when injecting @service_container as argument?

Asked

Viewed 58 times

2

In the archive services.yml I have the following service configured:

services:
    api.response_factory:
        class: AppBundle\Api\ResponseFactory
        arguments: ['@service_container']

In class ResponseFactory I have the constructor method that takes the argument:

namespace AppBundle\Api;

use Symfony\Component\DependencyInjection\ContainerInterface as Container;

public function __construct(Container $container)
{
    $this->container = $container;
}

Note that the argument container is the type ContainerInterface.

If it’s just an interface, how can the code below work?

$this->container->get('router')->generate('ROTA_A');

1 answer

1


The method says it accepts objects whose class implements the interface ContainerInterface. It doesn’t mean you’re getting an instance of an interface - that would be impossible.

In the case of the above code, you are receiving an instance of the class Container (which in turn implements the interface ContainerInterface). That’s why, from the container, you can get the service router and thus generate the desired route.

Another thing: avoid injecting service containers into other services; prefer to inject individual services, so your code is cleaner and concise.

For example, if you want to inject only the service router (which is the same argument when you use the method get container), create your service definition as follows:

services:
    api.response_factory:
        class: AppBundle\Api\ResponseFactory
        arguments: ['@router']

And then change the constructor to receive an object that implements the class RouterInterface:

<?php

namespace AppBundle\Api;

use Symfony\Component\Routing\RouterInterface;

class ResponseFactory
{
    /** @var RouterInterface $router */
    protected $router;

    /**
     * @param RouterInterface $router
     */
    public function __construct(RouterInterface $router)
    {
        $this->router = $router;
    }
}

You can see all the services available in your application (including the ones you created) through the command app/console container:debug (or bin/console debug:container in the case of Symfony 3 up).

  • Rodrigo, what do you mean, inject individual services? In my service I need to generate a route and I need the container for this, as would be a good practice for this?

  • I changed the answer to your question. :)

  • I admire who shows using Symfony :D

  • @Wallacemaxters my favorite framework since forever :)

  • @Wallacemaxters that was a compliment? kkk! Symfony is not solution to everything but serves very well for the projects I’m involved at the moment!

  • @Filipemoraes yes. The Framework is complex, but well done. Although I prefer the source code (the beauty of the code) of Laravel, I think that Symfony is very well structured. Even Laravel has a part that uses Symfony’s Httpfoundation. So whether I want to or not, I end up using Symfony :D

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.