我想使用以下代码将EntityManager实例传递给我的控制器的构造函数:
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 } }
我通过将参数放入service.yml文件来进行构造函数注入:
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包含在config.yml中,当我运行时
php bin/console debug:container app.user_controller
我明白了:
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 - ------------------ -------------------------------------
但是,调用映射到我的控制器的路由,我得到:
UserController.php第17行中的FatalThrowableError:类型错误:传递给AppBundle\Controller\UserController :: __ construct()的参数1必须是Doctrine\ORM\EntityManager的实例,没有给出,在/ home/michel/Documents/Terminfinder /中调用202号线上的vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Controller/ControllerResolver.php
我无法弄清楚,为什么EntityManager没有被注入?