The method says that it accepts objects whose class implements the ContainerInterface
interface. It does not mean that you are getting an instance of an interface - that would be impossible.
In the case of the above code, you are getting an instance of the Container
class (which, in turn, implements the ContainerInterface
interface). This is why, from the container, you can get the router
service and thus generate the desired route.
Another thing: avoid injecting service containers into other services; prefer to inject individual services, so your code gets cleaner and more concise.
For example, if you want to inject only the router
service (which is the same argument when you use the container get
method), 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 those you have created) by means of the command app/console container:debug
(or bin/console debug:container
in the case of Symfony 3 up).