【问题标题】:Error passing ContainerInterface as a parameter将 ContainerInterface 作为参数传递时出错
【发布时间】:2014-02-18 09:56:25
【问题描述】:

为什么我在尝试获取 ContainerInterface 时收到此错误:

ERROR - exception 'ErrorException' with message 'Catchable Fatal Error: Argument 5 passed to Project\InvitationsBundle\Invitations::__construct() must implement interface Symfony\Component\DependencyInjection\ContainerInterface, none given, called in /www/project/app/cache/dev/appDevDebugProjectContainer.php on line 1709 and defined in /www/project/src/Project/InvitationsBundle/Invitations.php line 23' 

并且在构造方法中添加ContainerInterface时出现错误:

<?php

namespace Pro\InvitationsBundle;

use Pro\CommunityBundle\Entity\Community;
use Pro\InvitationsBundle\Entity\Invitation;
use Doctrine\Bundle\DoctrineBundle\Registry;
use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Pro\UserBundle\Entity\User;
use Symfony\Component\DependencyInjection\ContainerInterface;

class Invitations
{
    private $mailer;
    private $translator;
    private $templating;
    private $doctrine;
    private $container;


    function __construct(\Swift_Mailer $mailer, TranslatorInterface $translator, EngineInterface $templating,
        Registry $doctrine, ContainerInterface $container)
    {
        $this->mailer = $mailer;
        $this->translator = $translator;
        $this->templating = $templating;
        $this->doctrine = $doctrine;
        $this->container = $container;
    }
    ...
}

【问题讨论】:

  • 请告诉我您的服务定义。
  • 是的,我认为这就是问题所在。我已经定义了下一个参数:[@mailer, @translator, @templating, @doctrine]

标签: php symfony service containers


【解决方案1】:

您可能有错误的服务定义。您应该注入所有 Invitations 参数,例如:

invitations:
    class: Redconvive\InvitationsBundle\Invitations
    arguments: [@mailer, @translator, @templating, @doctrine, @service_container]

您可能忘记了 @service_container

顺便说一句。请注意,注入整个容器是个坏主意。您应该只注入必要的服务。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-21
    • 1970-01-01
    • 1970-01-01
    • 2021-02-10
    • 1970-01-01
    • 2016-04-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多