【问题标题】:Symfony add doctrine to services.yml and custom extensionsSymfony 将教义添加到 services.yml 和自定义扩展
【发布时间】:2017-11-21 10:50:36
【问题描述】:

我想要实现的是调用学说 ini TwigExtension,我从 google 和 stackoverflow 获得了一些代码,如下所示:

service.yml

twig.extension:
        class: AppBundle\Twig\AppExtension
        arguments:
            doctrine : '@doctrine'
        tags:
          -  { name: twig.extension }

AppBundle\Twig\AppEtension.php

<?php

namespace AppBundle\Twig;

use Symfony\Bridge\Doctrine\RegistryInterface;

class AppExtension extends \Twig_Extension
{
    protected $doctrine;

    public function __construct(RegistryInterface $doctrine)
    {
        $this->doctrine = $doctrine;
    }

}

我已按照来源的所有说明进行操作,但我仍然遇到错误,无法在这里调用教义或使用 entitymanager 进行操作。

这是我的错误:

Symfony\Component\DependencyInjection\Exception\InvalidArgumentException]                                                                       
  Invalid key "doctrine" found in arguments of method "__construct()" for service "twig.extension": only integer or $named arguments are allowed.  

如何解决?

【问题讨论】:

  • 试试@doctrine.orm.entity_manager
  • @Jory 下面的回答是正确的解决方案。问题是您使用的是旧版本的 Symfony,但试图遵循最新的文档。在服务容器文档页面右上角选择正确的 Symfony 版本,看看区别。

标签: php symfony doctrine-orm doctrine


【解决方案1】:

service.yml 中,您应该使用$doctrine : '@doctrine' 而不是doctrine : '@doctrine'

您遇到的错误会尝试通过说 only integer or $named arguments are allowed 来告诉您这一点 - 请注意 **$** 命名的部分。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-03
    • 2014-11-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多