【问题标题】:symfony 2: using asset for image path in css [duplicate]symfony 2:在css中使用资产作为图像路径[重复]
【发布时间】:2013-08-02 16:59:56
【问题描述】:

我是 php 和 symfony 的新手。我一直在成功地使用以下语法将 css 和脚本包含到我的页面中:

<link rel="stylesheet" type="text/css" href="{{ asset('css/bootstrap.css') }}">

现在,我有一个引用图像的 css

.menu{
 background: url(../img/mobile.png) no-repeat center;
}

找不到图片。我认为这是因为我不能在这里使用asset() 语法。

我该如何解决这个问题?

【问题讨论】:

  • 你在哪里使用asset()?你能发布那个代码吗?
  • 是的,抱歉,我已经编辑了我的帖子。但问题是如何在 css 文件中执行此操作。

标签: php css symfony


【解决方案1】:

查看 web 文件夹,如果图像存在或不存在,因为您没有在 css 中使用资产,只是在 twig 中

您是否使用 assets:install web 生成图像以使图像存在于 web 文件夹中

【讨论】:

  • 图片在 web/img 文件夹中。我没有生成任何东西(我什至不知道 assets:install 是什么。我只是将 img 文件夹放到了 web 文件夹中。这还不够吗?
  • 不,这不是因为 img 文件夹应该位于 \web\bundles\nameofyourbundle exemple \web\bundles\AcmeExemple
【解决方案2】:

css重写过滤器可以解决这种情况:

{% stylesheets "css/bootstrap.css" filter="cssrewrite" %}
    <link rel="stylesheet" type="text/css" href="{{ asset_url }}" />
{% endstylesheets %}

【讨论】:

    【解决方案3】:

    试试这个:

    <link rel="stylesheet" type="text/css" href="{{ asset('bundles/YOUR_BUNDLE_NAME/css/bootstrap.css') }}">
    

    YOUR_BUNDLE_NAME 是 ex。 testbundle

    确保将bootstrap.css 文件放入/TestBundle/Resources/public/css 目录。

    执行命令:

    php app/console assets:install 
    

    此命令会将公用文件夹中的所有文件“硬拷贝”到可用的网络文件夹中。

    您的 css 字段路径现在将是 bundles/testbundle/css/bootstrap.css

    【讨论】:

      猜你喜欢
      • 2012-10-30
      • 1970-01-01
      • 1970-01-01
      • 2015-10-14
      • 2019-08-31
      • 2014-05-01
      • 2014-12-29
      • 2012-04-13
      • 1970-01-01
      相关资源
      最近更新 更多