如何修复“;将myBundle添加到asseticBundle配置中";symfony2异常


How do I fix "Add myBundle to the asseticBundle config" symfony2 exception?

当我尝试使用TWIG {% javascript %}标记链接到我的.js文件时,它会返回以下异常:

An exception has been thrown during the compilation of a template ("You must add CompetitiongameBundle to the assetic.bundle config to use the {% javascripts %} tag in CompetitiongameBundle:game:index.html.twig.") in "CompetitiongameBundle:game:index.html.twig".

我的index.html.twig看起来像:

{% javascripts 'CompetitiongameBundle/Resources/views/public/js/*'%}
    <script type="text/javascript" src="{{ asset_url }}" ></script>
{% endjavascripts %}
Hello {{ name }}!
<a href='{{ nexturl }}' >Login</a>

当我这样做时,我的捆绑包已经存在于配置文件中:

php app/console config:dump-reference assetic

我该怎么解决这个问题?

是的,我试过了,它为我解决了这个问题。对于一开始不知道如何添加的人(像我一样)来说:

  1. 编辑app/config/config.yml
  2. 然后转到assetic:
  3. 在assetic下:转到bundles: []
  4. 并在bundles: []//中键入您的捆绑包名称

例如,如果您的捆绑包是Acme'DemoBundle,则执行以下

assetic:
   bundles: [ AcmeDemoBundle ]

AcmeDemoBundle周围没有引号。就是这样。(Symfony2)

如果您希望assetic在默认情况下包括您的捆绑包,您可以(用#)注释行bundles: []

例如:

assetic:
    debug:          "%kernel.debug%"
    use_controller: false
    #bundles:        [ ]
    #java: /usr/bin/java

有时您需要即时做出决定,然后可以使用DependencyInjection。

例如加载和管理配置:

<?php
namespace You'ExampeBundle'DependencyInjection;
use Symfony'Component'DependencyInjection'ContainerBuilder;
/* ... */
class YouExampeExtension extends Extension
{
    /* ... */
    public function load(array $configs, ContainerBuilder $container)
    {
        /* ... */
        $aAsseticBundle = $container->getParameter('assetic.bundles');
        $aAsseticBundle[] = 'YouExampeBundle';
        $aAsseticBundle[] = 'AnotheBundle';
        $container->setParameter('assetic.bundles', $aAsseticBundle);
        /* ... */
    }
}

您可以使用更复杂的逻辑来操作配置(在合理的限制内)

您需要将您的bundle添加到app/config/config.yml文件(symfony 2.1)中assetic:部分的bundle:[]行