sonata管理资产的资产网址不正确

时间:2014-11-18 15:51:31

标签: symfony twig sonata-admin assetic

我在Symfony2中更改了配置,以便在AWS S3中转储资产。

# app/AppKernel.php

/**
 * {@inheritdoc}
 */
public function boot()
{
    parent::boot();

    $s3client = new \MyVendor\SiteBundle\Entity\S3AssetManager(
        $this->container->getParameter('aws_access_key'),
        $this->container->getParameter('aws_secret_key'),
        $this->container->getParameter('aws_s3_region')
    );

    $s3client->registerStreamWrapper();
}

# app/config_prod.yml
framework:
    templating:
        assets_base_url: "http://myAssetsHost.com/"

assetic:
    write_to: 's3://myAssetsHost.com'

以下是我在观看中使用的内容:

    {% block stylesheets %}
        <link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Open+Sans&subset=latin,greek" />
        {% stylesheets
        'bundles/mysite/css/screen.css'
        filter='cssrewrite' %}
        <link rel="stylesheet" href="{{ asset_url }}" />
        {% endstylesheets %}
    {% endblock %}

它在前端工作正常,我的网址是这样的:

  

http://myAssetsHost.com/css/cc04b97.css

在后端(/ admin / dashboard)中,网址生成如下:

  

http://myAssetsHost.com/bundles/sonataadmin/vendor/bootstrap/dist/css/bootstrap.min.css

导致404因为未生成文件。奏鸣曲(默认)standard_layout.html.twig:

        {% for stylesheet in admin_pool.getOption('stylesheets', []) %}
            <link rel="stylesheet" type="text/css"  href="{{ asset(stylesheet) }}"  />
        {% endfor %}

在部署期间,我使用这两个命令来转储资产:

  php app/console cache:clear --env=prod
  php app/console assetic:dump --env=prod

如何让Assetic获取所有bundle的资产以便将它们发布到S3?

1 个答案:

答案 0 :(得分:0)

以下是我如何解决它:

# app/config.yml
sonata_admin:
...
templates:
    layout:  SonataAdminBundle::standard_layout_override.html.twig

因为twig的asset()不会生成与资产stylesheets&amp; javascripts阻止,我创建了一个覆盖stylesheets&amp;的文件。 javascripts阻止:

# app/Resources/SonataAdminBundle/views/standard_layout_override.html.twig
{% extends 'SonataAdminBundle::standard_layout.html.twig' %}

{% block stylesheets %}
    {% stylesheets
        'bundles/sonataadmin/vendor/bootstrap/dist/css/bootstrap.min.css'
        'bundles/sonataadmin/vendor/AdminLTE/css/font-awesome.min.css'
        'bundles/sonataadmin/vendor/AdminLTE/css/ionicons.min.css'
        'bundles/sonataadmin/vendor/AdminLTE/css/AdminLTE.css'
        'bundles/sonatacore/vendor/eonasdan-bootstrap-datetimepicker/build/css/bootstrap-datetimepicker.min.css'
        'bundles/sonataadmin/vendor/jqueryui/themes/base/jquery-ui.css'
        'bundles/sonataadmin/vendor/select2/select2.css'
        'bundles/sonataadmin/vendor/select2/select2-bootstrap.css'
        'bundles/sonataadmin/vendor/x-editable/dist/bootstrap3-editable/css/bootstrap-editable.css'
        'bundles/sonataadmin/css/styles.css'
        'bundles/sonataadmin/css/layout.css'
        filter='cssrewrite' %}
        <link rel="stylesheet" href="{{ asset_url }}" />
    {% endstylesheets %}
{% endblock %}

{% block javascripts %}
    <script>
        window.SONATA_CONFIG = {
            CONFIRM_EXIT: {% if admin_pool is defined and admin_pool.getOption('confirm_exit') %}true{% else %}false{% endif %},
            USE_SELECT2: {% if admin_pool is defined and admin_pool.getOption('use_select2') %}true{% else %}false{% endif %},
            USE_ICHECK: {% if admin_pool is defined and admin_pool.getOption('use_icheck') %}true{% else %}false{% endif %}
        };
        window.SONATA_TRANSLATIONS = {
            CONFIRM_EXIT:  '{{ 'confirm_exit'|trans({}, 'SonataAdminBundle')|escape('js') }}'
        };
        </script>

    {% javascripts
        'bundles/sonataadmin/vendor/jquery/dist/jquery.min.js'
        'bundles/sonataadmin/vendor/jquery.scrollTo/jquery.scrollTo.min.js'
        'bundles/sonatacore/vendor/moment/min/moment.min.js'
        'bundles/sonataadmin/vendor/bootstrap/dist/js/bootstrap.min.js'
        'bundles/sonatacore/vendor/eonasdan-bootstrap-datetimepicker/build/js/bootstrap-datetimepicker.min.js'
        'bundles/sonataadmin/vendor/jqueryui/ui/minified/jquery-ui.min.js'
        'bundles/sonataadmin/vendor/jqueryui/ui/minified/i18n/jquery-ui-i18n.min.js'
        'bundles/sonataadmin/jquery/jquery.form.js'
        'bundles/sonataadmin/jquery/jquery.confirmExit.js'
        'bundles/sonataadmin/vendor/x-editable/dist/bootstrap3-editable/js/bootstrap-editable.min.js'
        'bundles/sonataadmin/vendor/select2/select2.min.js'
        'bundles/sonataadmin/App.js'
        'bundles/sonataadmin/Admin.js'
    %}
    <script type="text/javascript" src="{{ asset_url }}"></script>
    {% endjavascripts %}
{% endblock %}

请注意,它不会调用{{ parent() }},因为我不想加载不存在的文件。

这种方法的唯一问题是它完全忽略了

admin_pool.getOption('stylesheets', []) admin_pool.getOption('javascripts', [])

因此,如果在下一个SonataAdminBundle版本中添加了新的样式表/ js文件,我将不得不密切关注并更新我的文件。

不幸的是,{% stylesheets %}{% javascripts %}都无法处理数组。如果可能,你可以这样做:

{% stylesheets filter='cssrewrite'  
admin_pool.getOption('stylesheets', []) %}
<link rel="stylesheet" href="{{ asset_url }}" />
{% endstylesheets %}