【问题标题】:Symfony loading assets from templateSymfony 从模板加载资产
【发布时间】:2018-03-14 20:17:50
【问题描述】:

我无法在 Symfony 中链接我的风格。

<link rel="stylesheet" href="{{ asset('assets/styles/css/global.min.css') }}" />

我的 CSS 文件在 assets 目录中

  • 是的,我已经从 symfony 下载了资源
  • 是的,我尝试过更改目录

【问题讨论】:

  • 您是否发现任何错误?
  • 清除缓存(删除文件夹 var/cache/* 或 php bin/console c:c -e prod)
  • 如果您不使用 symfony encore 来管理您的资产,请将它们存储在您的公共目录中。并使用以下方式调用您的资产:{{ asset('styles/css/global.min.css') }}(您的样式目录是公开的)

标签: css symfony assets


【解决方案1】:

在 SF3 中,您应该将资产存储在

Store your assets in the web/ directory.

https://symfony.com/doc/3.4/best_practices/web-assets.html

在 SF4 中,您应该将资产存储在

Store your assets in the assets/ directory at the root of your project.

并使用 Webpack Encore 编译、组合和最小化 Web 资产。

https://symfony.com/doc/current/best_practices/web-assets.html

这是我的配置(SF3): config_dev.yml

framework:
# Assets CDN & dev paths
# change here and in the config_prod.xml
assets:
    packages:
        avatar:
            base_path: upload/members/pics/
        app_css:
            base_path: css/
        app_img:
            base_path: img/
        ....

config_prod.yml - if you want to use CDN

framework:
# Assets CDN & dev paths
# change here and in the config_prod.xml
assets:
    packages:
        avatar:
            base_path: xx.xx.xx.xx/upload/members/pics/ (IP Address of CDN)
        app_css:
            base_path: xx.xx.xx.xx/css/(IP Address of CDN)
        app_img:
            base_path: xx.xx.xx.xx/img/ (IP Address of CDN)
        ....

在树枝上:

    <a href="{{ path('homepage') }}" class="logo" style="background-image:url({{ asset('logo.svg', 'app_img') }})"></a>

【讨论】:

    【解决方案2】:

    在编译之前,您的资产必须位于 Asset 文件夹中。编译后的版本应该在web文件夹(SF 3不带flex)或public文件夹(sf4带flex)

    【讨论】:

      猜你喜欢
      • 2023-03-19
      • 2017-06-06
      • 2012-11-12
      • 1970-01-01
      • 2011-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多