【问题标题】:Asset_url in production environment Symfony 3生产环境 Symfony 3 中的 Asset_url
【发布时间】:2016-11-15 14:56:31
【问题描述】:

我对我生成的资产 URL 有疑问。

在视图上,这就是我所做的:

{% block stylesheets %}
        {% stylesheets
            'assets/vendor/bootstrap/dist/css/bootstrap.min.css'
            'assets/vendor/font-awesome/css/font-awesome.min.css'
            'assets/vendor/dist/css/*.css'
            output = 'bundles/compiled/css/app.css' %}
            <link rel="stylesheet" href="{{ asset_url }}" />
        {% endstylesheets %}
{% endblock %}

{% javascripts
            'assets/vendor/jquery/dist/jquery.min.js'
            'assets/vendor/bootstrap/dist/js/bootstrap.min.js'
            'assets/vendor/dist/js/wow.min.js'
            'assets/vendor/dist/js/slider.js'
            'assets/vendor/dist/js/jquery.fancybox.js'
            'assets/vendor/dist/js/owl.carousel.min.js'
            'assets/vendor/dist/js/main.js'
            'assets/vendor/dist/js/modernizr-2.6.2.min.js' 
            output = 'bundles/compiled/js/app.js' %}
            <script src="{{ asset_url }}"></script>
{% endjavascripts %}

在开发环境中,应用不会调用assetic:dump生成的这些输出链接,即:

bundles/compiled/css/app.css

bundles/compiled/js/app.js/

都在 /web 文件夹之后。

在 prod 中,我无法到达这些位置。我得到和 404 以http://link.com/bundles/compiled/css/app.css 为例。

那么,如何配置我的config.yml 以在prod 环境中到达该路径? 这就是我所拥有的:

assetic:
    debug:          '%kernel.debug%'
    use_controller: '%kernel.debug%'
    filters:
cssrewrite: ~

**编辑:**我发现了问题。我assetic的配置没问题,问题出在VirtualHosts文件(具体来说是modrewrite行),它只接受“.php”扩展文件和web/文件夹权限。

【问题讨论】:

  • 您是否运行了命令php app/console assets:install
  • @Veve 是的,在assetic:dump 之前。

标签: css symfony assets environment


【解决方案1】:

这就是使用AsseticBundle 的全部意义所在!当您处于prod 模式时,您需要转储资产文件。看看here

php bin/console cache:clear --env=prod //clear the cache for prod environment
php bin/console assetic:dump --env=prod --no-debug

结果将是所有css 样式表在一个结果文件(bundles/compiled/css/app.css) 中的串联,以及所有js 脚本在一个结果文件(bundles/compiled/js/app.js) 中的串联,例如reducing the requests to the server 的原因。 此时,您将无法在页面的源代码中看到所有 css + js 文件,除了您的 compiled/ 目录中的这两个文件。

当您处于dev 模式时,AsseticBundle 的默认行为是生成所有 css + js 文件,用于调试目的。也就是说,帮助您轻松调试代码。所以此时您将能够看到所有 css+js 文件,但看不到 compiled/ 目录中的两个文件。

请注意,cssrewrite 必须是 filters 的子代。请参阅here您需要的更正。

【讨论】:

  • 我做了所有这些说明。但请继续根据文件请求接受 404。在生产应用程序的 HTML 中,我有 &lt;link rel="stylesheet" href="/css/b61ff27.css" /&gt;。我做错了什么?
  • @gamoridev 我通常在转储控制台命令中添加web/目录:$ php bin/console assetic:dump web/,这样那些路径才有效。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-28
  • 2014-11-11
  • 1970-01-01
相关资源
最近更新 更多