php - 将 ContainerInterface 作为参数传递时出错

标签 php symfony service containers

为什么在尝试获取 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;
    }
    ...
}

最佳答案

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

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

您可能忘记了 @service_container

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

关于php - 将 ContainerInterface 作为参数传递时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21850043/

相关文章:

Symfony2和Doctrine 2结果数据错误地返回空数组

php - 将一组对象从一个 Controller 传递到另一个 Controller 。交响乐2

php - 复选框重复

php - 将 CSS 应用于所有页面,包括 http ://url?/id=x

php - Symfony 3,形式中的多对一关系

java - 在 Android Studio 中将数据从 onReceive 传输到 SQL 数据库

java.lang.NoClassDefFoundError : ajavaservice/DemoService

android - 在 Android 中解锁设备屏幕时启动服务

PHP 在每 4 条记录后循环添加 <div "style="clear"> </div>

php - 使用 swift 和 API 进行 POST