【问题标题】:How do I fix "Add myBundle to the asseticBundle config" symfony2 exception?如何修复“将 myBundle 添加到asseticBundle 配置”symfony2 异常?
【发布时间】:2012-08-26 17:13:18
【问题描述】:

当我尝试使用 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>

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

php app/console config:dump-reference assetic

我该如何解决这个问题?

【问题讨论】:

标签: php symfony twig


【解决方案1】:

是的,我试过了,它为我解决了这个问题。对于最初不知道如何添加的人(比如我),然后只是:

  1. 编辑app/config/config.yml
  2. 那就去assetic:
  3. 资产下:去bundles: []
  4. bundles: [] //输入你的包名

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

assetic:
   bundles: [ AcmeDemoBundle ]

AcmeDemoBundle 周围没有引号。而已。 (Symfony2)

【讨论】:

  • 如果需要,额外的捆绑包应该用逗号分隔。
  • 我正在使用 symfony2.3,我将我的包添加到数组中,但我仍然有同样的异常。即使我评论捆绑包时,我也会收到相同的异常消息。我该怎么办?
  • 如果您正在迁移到 prod 并遇到此问题,请不要忘记清理缓存并转储资产:" php app/console cache:clear --env=prod --no -debug" 和 "php app/consoleassetic:dump --env=prod --no-debug"
  • @Dev DOS 你找到解决方案了吗?
【解决方案2】:

如果您希望assetic 默认包含您的捆绑包,您可以注释(带#)行bundles: []

例如:

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

【讨论】:

  • 评论包有副作用吗?
  • 我想不到。它只是让资产包含您项目中的所有捆绑包。也许它会减慢您的应用程序,但我个人从未注意到速度上有任何差异。
【解决方案3】:

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

例如给loads and manages configuration

<?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 继承扩展另一个 Bundle,这将特别有用。
【解决方案4】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-15
    • 1970-01-01
    • 2018-09-28
    • 2014-05-01
    • 2012-05-29
    相关资源
    最近更新 更多