【问题标题】:Symfony2: service container didn't pass to controller constructorSymfony2:服务容器没有传递给控制器​​构造函数
【发布时间】: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


【解决方案1】:

你是如何在你的路由中配置你的控制器的?

我感觉您没有使用“控制器即服务”表示法:

my_controller:
    pattern:   /
    defaults:  { _controller: shop.website.index_controller:indexAction }

此语法与默认路由语法不同。看看here

【讨论】:

  • 我的控制器操作处理请求 - 将依赖传递给它的问题
  • @MagicElephant 我明白你的问题了……如果你想让我帮助你,请重新阅读我的答案,告诉我你是如何配置你的路线的……
【解决方案2】:

可能的原因是下一个:

SF2 直接调用你的类构造函数。但是您应该说将其作为服务获取(以调用/参数的形式提供所有服务选项)

添加

/**
 * @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

在页面http://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/routing.html#controller-as-service重新阅读此内容

并注意马蒂厄那不勒斯的回答

【讨论】:

    【解决方案3】:

    您不需要扩展任何其他文件/控制器。

    先删除扩展。

    添加:

    use Symfony\Component\DependencyInjection\ContainerInterface;
    

    替换:

    Container $container
    

    ContainerInterface $container
    

    【讨论】:

    • 相同的可捕获致命错误:传递给 Shop\CommonBundle\Controller\CommonController::__construct() 的参数 1 必须实现接口 Symfony\Component\DependencyInjection\ContainerInterface,没有给出
    • 请在您的问题下方添加新代码,以便我们查看问题所在。
    【解决方案4】:

    为什么要将容器注入控制器?

    只需扩展 Symfony\Bundle\FrameworkBundle\Controller\Controller,您就可以像这样访问任何服务:

        namespace AppBundle\Controller;
    
        use Symfony\Bundle\FrameworkBundle\Controller\Controller;
    
        class HelloController extends Controller
        {
            public function indexAction()
            {
                // ...
                $myService = $this->get('my_service');
            }
        }
    

    【讨论】:

    • 对不起。但是我尝试创建没有扩展内部 symfony 控制器的自我控制器 - 所以我需要自己实现 get 方法 - 这就是传递容器的原因
    猜你喜欢
    • 2013-10-09
    • 1970-01-01
    • 1970-01-01
    • 2015-01-28
    • 1970-01-01
    • 2010-12-21
    • 1970-01-01
    • 1970-01-01
    • 2017-07-26
    相关资源
    最近更新 更多