【发布时间】:2015-03-12 21:06:28
【问题描述】:
为什么会出现这个错误?
可捕获的致命错误:传递给 Application\Sonata\ProductBundle\Controller\ProductAdminController::__construct() 的参数 1 必须是 ContainerInterface 的实例,给定 appDevDebugProjectContainer 的实例
这是我的 services.yml:
services:
product_admin_controller:
class: Application\Sonata\ProductBundle\Controller\ProductAdminController
arguments: ["@service_container"]
tags:
- { name: doctrine.event_listener, event: postLoad, connection: default }
还有我的控制器:
class ProductAdminController extends Controller
{
protected $container;
public function __construct(\ContainerInterface $container)
{
$this->container = $container;
}
}
【问题讨论】:
-
这似乎是
Symfony的经典控制器和控制器即服务概念的混合体。为什么你们既要扩展Controller又要通过__construct传递Container? -
这是一个命名空间问题。使用 Symfony\Component\DependencyInjection\ContainerInterface; __construct(ContainerInterface。你真的应该使用 ContainerAware 接口。更好的是,注入你的特定依赖项而不是完整的容器。
-
感谢大家的帮助。主要目标是覆盖 orm 产品类并使用 mongodb odm 驱动的变体对象对其进行扩展。我尝试从第 32 页复制到以下内容 (fr.slideshare.net/jwage/…)。所以第一次尝试是注入 EntityManager (stackoverflow.com/questions/20587354/…) 但我无法做到没有错误所以我尝试了 (stackoverflow.com/questions/22128402/…)
标签: symfony dependency-injection sonata