7
votes

I want to pass the EntityManager instance into the constructor of my controller, using this code:

namespace AppBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Doctrine\ORM\EntityManager;

class UserController extends Controller
{

    public function __construct( EntityManager $entityManager )
    {
        // do some stuff with the entityManager
    }
}

I do the constructor injection by putting the parameters into the service.yml file:

parameters:
#    parameter_name: value

services:
#    service_name:
#        class: AppBundle\Directory\ClassName
#        arguments: ["@another_service_name", "plain_value", "%parameter_name%"]
    app.user_controller:
        class: AppBundle\Controller\UserController
        arguments: ['@doctrine.orm.entity_manager']

the service.yml is included in the config.yml and when I run

php bin/console debug:container app.user_controller

I get:

 Information for Service "app.user_controller"
 =============================================

 ------------------ ------------------------------------- 
  Option             Value                                
 ------------------ ------------------------------------- 
  Service ID         app.user_controller                  
  Class              AppBundle\Controller\UserController  
  Tags               -                                    
  Public             yes                                  
  Synthetic          no                                   
  Lazy               no                                   
  Shared             yes                                  
  Abstract           no                                   
  Autowired          no                                   
  Autowiring Types   -                                    
 ------------------ ------------------------------------- 

However, calling a route which is mapped to my controller, I get:

FatalThrowableError in UserController.php line 17: Type error: Argument 1 passed to AppBundle\Controller\UserController::__construct() must be an instance of Doctrine\ORM\EntityManager, none given, called in /home/michel/Documents/Terminfinder/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Controller/ControllerResolver.php on line 202

I cant figure out, why the EntityManager is not getting injected?

4
Have you cleared the cache?lxg
@lxg I cleared the cache using php bin/console cache:clear --env=dev and manually deleted var/cache/dev/* as wellltsstar
Declaring controllers as services is not considered a good practices by fabpot himself: github.com/symfony/symfony-docs/issues/457 . I agree with this.COil
@COil I see your point, but there is an official documentation article about how to use controllers as services. symfony.com/doc/current/controller/service.htmlltsstar
You need to configure the route to use the controller as a service: symfony.com/doc/current/controller/… And ignore the naysayers. Perfectly fine to define controllers as services. If you still have trouble then update the question with your route.Cerad

4 Answers

5
votes

When using the base classController.php the Container is usually auto-wired by the framework in theControllerResolver.

Basically you are trying to mix up how things actually work.

To solve your problem you basically have two solutions:

  1. Do no try to inject the dependency but fetch it directly from the Container from within your action/method.

public function listUsers(Request $request) { $em = $this->container->get('doctrine.orm.entity_manager'); }

  1. Create a controller manually but not extend the Controller base class; and set ip up as a service

To go a bit further on this point, some people will advise to do not use the default Controller provided by Symfony.

While I totally understand their point of view, I'm slightly more moderated on the subject.

The idea behind injecting only the required dependencies is to avoid and force people to have thin controller, which is a good thing.

However, with a little of auto-determination, using the existing shortcut is much simpler.

A Controller / Action is nothing more but the glue between your Views and your Domain/Models.

Prevent yourself from doing too much in your Controller using the ContainerAware facility.

A Controller can thrown away without generate business changes in your system.

2
votes

Since 2017 and Symfony 3.3+, there is native support for controllers as services.

You can keep your controller the way it is, since you're using constructor injection correctly.

Just modify your services.yml:

# app/config/services.yml

services:
    _defaults:
        autowire: true

    AppBundle\:
        resouces: ../../src/AppBundle

It will:

  • load all controllers and repositories as services
  • autowire contructor dependencies (in your case EntityManager)


Step further: repositories as services

Ther were many question on SO regarding Doctrine + repository + service + controller, so I've put down one general answer to a post. Definitelly check if you prefer constructor injection and services over static and service locators.

-1
votes

Did you use following pattern to call the controller AppBundle:Default:index? if yes that should be the problem. If you want to use controller as a service you have to use the pattern: app.controller_id:indexAction which uses the id of the service to load the controller.

Otherwise it will try to create an instance of the class without using the service container.

For more information see the symfony documentation about this topic https://symfony.com/doc/current/controller/service.html

-4
votes

The entity manager is available in a controller without needing to inject it. All it takes is:

$em = $this->getDoctrine()->getManager();