symfony 2:在css中使用asset作为图像路径


symfony 2: using asset for image path in css

我是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()语法。

我该如何解决这个问题?

查看web文件夹,看看图像是否存在,因为您没有在css中使用资产,只在trick 中使用

您是否使用assets生成了图像:安装web以使图像存在于web文件夹

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

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

只需尝试以下操作:

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

其中YOUR_BUNDLE_NAME是例如testbundle

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

执行命令:

php app/console assets:install 

此命令将把公用文件夹中的所有文件"硬拷贝"到可用的web文件夹中。

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