duanliang8464 2014-11-29 19:07
浏览 23
已采纳

Symfony2:服务容器未传递给控制器​​构造函数

Yep - i've search an answer and spend several hours with google. But trouble still actual

class IndexController extends Controller\CommonController
{
    private $container;
    public function __construct(Container $container) {
        $this->container = $container;
    }

in config.yml

shop.website.index_controller:
    class: %shop.website.index_controller%
    parent: shop.common.common_controller
    arguments:  [@service_container]

and

Catchable Fatal Error: Argument 1 passed to Shop\WebSiteBundle\Controller\IndexController::__construct() must implement interface Symfony\Component\DependencyInjection\ContainerInterface, none given, called in I:\sf2\www\vendor\symfony\symfony\src\Symfony\Bundle\FrameworkBundle\Controller\ControllerResolver.php on line 77 and defined in I:\sf2\www\src\Shop\WebSiteBundle\Controller\IndexController.php line 13

Can anybody explain where is the error?

Configuration in yml / annotaions please ( Cause different types for configuring make me crazy )

Thanks in advance

P.S> Updated code id

services:
    shop.common.common_controller:
        abstract: true
        class: %shop.common.common_controller%
        arguments: ["@templating"]

and

use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;

class CommonController
{
    protected $templating;

    public function __construct(EngineInterface $templating)
    {
        $this->templating = $templating;
    }

the same result (

  • 写回答

4条回答 默认 最新

  • douchan6512 2014-12-03 07:54
    关注

    Posilbe reason is next:

    SF2 call your class constructor directly. But you should say to get it as service ( to provide all service options as calls / arguments )

    Add

    /**
     * @Route(service="shop.website.index_controller")
     */
    class IndexController extends Controller\CommonController
    {
        private $container;
        public function __construct(Container $container) {
            $this->container = $container;
        }shop.website.index_controller
    

    Rew this at page http://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/routing.html#controller-as-service

    And pay attention to Matthieu Napoli answer

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置