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

Vim*_*deo 84 php symfony twig

当我尝试使用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".
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

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

php app/console config:dump-reference assetic
Run Code Online (Sandbox Code Playgroud)

我怎样才能解决这个问题 ?

小智 175

是的,我试过,它解决了我的问题.对于那些最初不知道如何添加的人(比如我)而言:

  1. 编辑 app/config/config.yml
  2. 然后去 assetic:
  3. 在资产下:去 bundles: []
  4. 并在bundles: []//键入您的包名称

例如,如果您的捆绑包是Acme\DemoBundle,请执行以下操作

assetic:
   bundles: [ AcmeDemoBundle ]
Run Code Online (Sandbox Code Playgroud)

周围没有报价AcmeDemoBundle.而已.(Symfony2的)

  • 如果需要,应使用逗号分隔其他包. (14认同)

Tiv*_*vie 24

如果您希望assetic默认包含您的软件包,您可以对该行进行注释(使用#) bundles: []

例如:

assetic:
    debug:          "%kernel.debug%"
    use_controller: false
    #bundles:        [ ]
    #java: /usr/bin/java
Run Code Online (Sandbox Code Playgroud)


Мак*_*тов 10

有时你需要动态做出决定,然后你可以使用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);

        /* ... */
    }
}
Run Code Online (Sandbox Code Playgroud)

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