【发布时间】:2014-11-29 19:07:34
【问题描述】:
是的 - 我已经搜索了一个答案并在谷歌上花了几个小时。但问题仍然存在
class IndexController extends Controller\CommonController
{
private $container;
public function __construct(Container $container) {
$this->container = $container;
}
在 config.yml 中
shop.website.index_controller:
class: %shop.website.index_controller%
parent: shop.common.common_controller
arguments: [@service_container]
和
可捕获的致命错误:参数 1 传递给 Shop\WebSiteBundle\Controller\IndexController::__construct() 必须 实现接口 Symfony\Component\DependencyInjection\ContainerInterface,没有给出, 叫进来 我:\sf2\www\vendor\symfony\symfony\src\Symfony\Bundle\FrameworkBundle\Controller\ControllerResolver.php 在第 77 行并在 我:\sf2\www\src\Shop\WebSiteBundle\Controller\IndexController.php 行 13
谁能解释错误在哪里?
请在 yml / annotaions 中进行配置(导致不同类型的配置让我抓狂)
提前致谢
P.S> 更新代码 id
services:
shop.common.common_controller:
abstract: true
class: %shop.common.common_controller%
arguments: ["@templating"]
和
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
class CommonController
{
protected $templating;
public function __construct(EngineInterface $templating)
{
$this->templating = $templating;
}
同样的结果(
【问题讨论】:
-
我尝试创建没有扩展内部 symfony 控制器的自我控制器 - 所以我需要自己实现 get 方法 - 这就是传递容器的原因
-
我建议检查 ControllerAutowire 捆绑包,这确实简化了整个过程:tomasvotruba.cz/blog/2016/03/10/…
标签: php symfony dependency-injection controller