如何在Symfony2中结合这些资产?

Pol*_*ino 5 symfony assetic

我正在使用ExposeTranslationBundle(将翻译暴露给javascript)和JMSI18nRoutingBundle(将路由暴露给javascript).这是我的<head>标签的一部分:

    {% javascripts filter='?yui_js' output='js/app.js'
        '../app/Resources/public/js/jquery-*.js'
        '../app/Resources/public/js/jquery/*'
        '../app/Resources/public/js/app.js'
        'bundles/fosjsrouting/js/router.js'
        'bundles/bazingaexposetranslation/js/translation.js' %}
        <script src="{{ asset_url }}" ></script>
    {% endjavascripts %}

    <!-- ExposeTranslationBundle and JMSI18nRoutingBundle -->
    <script src="{{ path('fos_js_routing_js',
        {"callback": "fos.Router.setData"}) }}"></script>
    <script src="{{ url('bazinga_exposetranslation_js') }}"></script>
Run Code Online (Sandbox Code Playgroud)

有可能将最后两个<script>进口组合成第一个资产,如何?

roo*_*m13 2

我认为这是不可能的,因为 FOSJSRouting javascript 文件是由控制器生成的。在内部,bundle 会缓存 js,但在 app/cache 中,因此每个请求都需要经过控制器。我不熟悉公开翻译包,但我想这里也是同样的问题。

github上FOSJsRouterBundle的issue tracke一直在讨论,也有解决方案。请在此处查看完整问题:https ://github.com/FriendsOfSymfony/FOSJsRoutingBundle/issues/22

解决方法是使用脚本或命令将输出转储到 web/js 目录中的文件中:

<?php

require_once __DIR__.'/../app/bootstrap.php.cache';
require_once __DIR__.'/../app/AppKernel.php';

use Symfony\Component\HttpFoundation\Request;

$kernel = new AppKernel('stage', false);
$kernel->loadClassCache();
$response = $kernel->handle(Request::create('/js/routing?callback=fos.Router.setData'));

file_put_contents(__DIR__.'/../web/js/routes.js', $response->getContent());
Run Code Online (Sandbox Code Playgroud)

这在某种程度上是一种解决方案。我一直在考虑实现一个通用捆绑包,可以使用控制器输出 js 为其他几个捆绑包配置此任务。控制器操作必须在 yml 文件中配置,然后必须在每次部署/修改路由/字符串时执行命令。但我还没有时间……还没有;)