【问题标题】:Services not loaded at Resources/config/services.yml, why?服务未在 Resources/config/services.yml 加载,为什么?
【发布时间】:2015-04-14 17:24:13
【问题描述】:

我在这里遇到了一个问题,我不知道为什么或在哪里失败,也许我错过了一些配置,无论如何,我在DependencyInjection\AppExtension.php 文件中有此代码:

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;

class AppExtension extends Extension
{
    /**
     * {@inheritdoc}
     */
    public function load(array $configs, ContainerBuilder $container)
    {
        $configuration = new Configuration();
        $config = $this->processConfiguration($configuration, $configs);

        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
        $loader->load('services.yml');
    }
}

然后在Resources/config/services.yml 我有这个:

services:
    pdone.twig.extension:
        class: GroupDCA\PDOneBundle\Extension\PDOneTwigExtension
        tags:
            -  { name: twig.extension }

由于某种原因无法正常工作。这意味着我收到了这个错误:

第 1 行的 PDOneBundle::pdone.html.twig 中不存在过滤器“empty”

现在,如果我将服务定义移动到 config/config.yml,则会收到此错误:

编译错误:无法对表达式的结果使用 isset()(您可以改用“null !== expression”)

这让我认为捆绑包没有通过 DependecyInjection,我在这里缺少什么?为什么会出现不同的错误?

【问题讨论】:

    标签: php symfony dependency-injection symfony-2.7


    【解决方案1】:

    1) 您是否将捆绑包添加到AppKernel

    2) 我不确定,但我认为您必须遵循 Extension 类的命名约定:

    • Bundle 的根目录应该包含DependencyInjection 目录
    • DependencyInjection 中,Extension 类应命名为<BUNDLE>Extension,不带“Bundle”后缀。在你的情况下是PDOOneExtension

    【讨论】:

    • Ups,命名约定我犯了一个错误AppExtension 应该是PDOneExtension 它现在可以工作了
    猜你喜欢
    • 1970-01-01
    • 2011-05-10
    • 2013-05-14
    • 2018-02-28
    • 1970-01-01
    • 2016-11-26
    • 2017-05-19
    • 2017-08-25
    • 1970-01-01
    相关资源
    最近更新 更多