Embedding Symfony3 controller constructor does not work

I want to pass an instance of EntityManager to 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 am doing a constructor injection by putting the parameters in 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']

service.yml is included in 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, by invoking a route that maps to my controller, I get:

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

I can’t understand why EntityManager is not being entered?

+4
4

Controller.php Container ControllerResolver.

, .

, :

  • , /.

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

  1. , ; ip

, , Symfony.

, .

, , .

, , .

A Controller/Action - , Views Domain/Models.

Controller ContainerAware.

A Controller .

+3

2017 Symfony 3.3 + .

, , .

services.yml:

# app/config/services.yml

services:
    _defaults:
        autowire: true

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

:

  • autowire ( EntityManager)


:

SO Doctrine + repository + service + controller, , . , .

0

AppBundle:Default:index? , . , : app.controller_id:indexAction, .

.

For more information, see the symfony documentation on this subject https://symfony.com/doc/current/controller/service.html

0
source

The object manager is available in the controller without requiring its entry. All that is required:

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

-3
source

Source: https://habr.com/ru/post/1667432/


All Articles