0

I'd like to use a constructor in my Symfony2 controller, but I have this error:

ContextErrorException: Catchable Fatal Error: Argument 1 passed to Acme\ApplicationBundle\Controller\CalendarActivityController::__construct() must implement interface Symfony\Component\DependencyInjection\ContainerInterface, none given, called in /Users/root/Documents/projects/Acme/Symfony/app/cache/dev/classes.php on line 2374 and defined in /Users/root/Documents/projects/Test/Symfony/src/Acme/ApplicationBundle/Controller/CalendarActivityController.php line 16

My constructor:

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

I must use a constructor because sometimes I'm using my controller as a service, and in this case container is not defined.

4
  • Guess you should decouple them anyways. Commented Mar 21, 2014 at 16:42
  • (Container $container = null)? Commented Mar 21, 2014 at 16:43
  • His solution got rid of the error message but you will still have problems the first time you try to use the container. It's not available in the constructor. You need to use setter injection to inject the container. More than likely you really want to use a listener instead of a controller. Plenty of threads on this. Commented Mar 21, 2014 at 16:59
  • After reading through the responses my suggestion is hacky… consider the other answers as better advice. Commented Mar 21, 2014 at 21:02

2 Answers 2

1

It is possible to inject services into your controller if your controller is registered as a service itself. Read more about it in the official docs: http://symfony.com/doc/current/cookbook/controller/service.html

However, let me point out, that there's not much value in injecting the Container. It's much easier to extend the Symfony\Component\DependencyInjection\ContainerAware. Symfony will inject the container for you automatically (since ContainerAware implements the ContainerAwareInterface).

Register your controller as a service if you want to inject specific services.

Use ContainerAware otherwise.

Sign up to request clarification or add additional context in comments.

2 Comments

Are you sure about the automatic container injection? I tried it once but no luck. I just spell out the setter injection in the services file. Maybe a tag is needed?
@Cerad, yes Symfony will inject container into your controller if you make it ContainerAware (implement the interface or extend the ContainerAware class). This is how it works by default in Symfony, also with automatically generated controllers.
0

you have to call container from dependncy injection use USE

    use Symfony\Component\DependencyInjection\ContainerInterface as Container;
      private $container;

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

    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.