Symfony-无法注册扩展名"AppBundle \ Twig \ Extension \ FileExtension";因为它已经被注册 [英] Symfony - Unable to register extension "AppBundle\Twig\Extension\FileExtension" as it is already registered

查看:104
本文介绍了Symfony-无法注册扩展名"AppBundle \ Twig \ Extension \ FileExtension";因为它已经被注册的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试在Symfony2-project中实现自定义Twig扩展时,出现以下错误:

When I try to implement custom Twig Extension in Symfony2-project, I get following error:

无法注册扩展名"AppBundle \ Twig \ Extension \ FileExtension" 因为它已经被注册.

Unable to register extension "AppBundle\Twig\Extension\FileExtension" as it is already registered.

在我的app/config/services.yml中,我有以下内容:

In my app/config/services.yml, I have following:

parameters:
    app.file.twig.extension.class: AppBundle\Twig\Extension\FileExtension

services:
    app.twig.file_extension:
        class: '%app.file.twig.extension.class%'
        tags:
            - { name: 'twig.extension' }

在AppBundle/Twig/Extensions/FileExtension中,我具有:

In AppBundle/Twig/Extensions/FileExtension, i have:

<?php

namespace AppBundle\Twig\Extension;

class FileExtension extends \Twig_Extension
{
    /**
     * Return the functions registered as twig extensions
     * 
     * @return array
     */
    public function getFunctions()
    {
        return array(
            new \Twig_SimpleFunction('file_exists', array($this, 'file_exists')),
        );
    }

    public function getName()
    {
        return 'app_file';
    }
}
?>

我试图清除缓存,但无法正常工作.有人知道为什么Twig会渲染两次吗?

I tried to clear cache, but not working. Anybody ideas why Twig renders twice?

推荐答案

@Cerad 所述,从Symfony开始 3.3 ,将自动加载树枝扩展名,无需在默认的services.yml中定义任何内容.

As @Cerad said, starting with Symfony 3.3, the twig extensions are automatically loaded and there is no need to define anything in the default services.yml.

根据官方文档:

如果您使用默认的services.yml配置,那么就完成了! Symfony将自动了解您的新服务并添加标签.

If you're using the default services.yml configuration, you're done! Symfony will automatically know about your new service and add the tag.

这篇关于Symfony-无法注册扩展名"AppBundle \ Twig \ Extension \ FileExtension";因为它已经被注册的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆