dongyao2022 2016-02-09 14:05
浏览 53
已采纳

app / Resources中的Symfony资产cssrewrite图像路径

I've got a problem with assetic and the path of my images in CSS on Symfony. My base template, my base CSS, and the images used in this CSS are in the folder app/Resources.

I have this:

app/
    Resources/
        public/
            css/
                base.css
            images/
                mybackground.png
    views/
        base.html.twig

In the file base.css, I want to load the file mybackground.css as a background, so I have this line:

background-image: url('../images/mybackground.png');

And in the file 'base.html.twig', I load the css with:

{% stylesheets '@base_css' filter='cssrewrite' %}

<link rel="stylesheet" type="text/css" charset="utf-8" media="all" href="{{ asset_url }}" />

{% endstylesheets %}

I also tried without the filter cssrewrite.

In app/config/config.yml, I have this:

assetic:
filters:
    cssrewrite: ~
assets:
    base_css:
        inputs:
            - '../app/Resources/public/css/base.css'

Before try, I used all of these commands:

php app/console cache:clear
php app/console cache:clear --env=prod
php app/console assetic:dump
php app/console assetic:dump --env=prod --no-debug
php app/console assets:install --symlink web

As you can guess, my background image isn't loaded (the browser returns a 404 not found error). I searched a lot on internet, and I found similar cases but with the resources in some bundles not in app/Resources.

  • 写回答

2条回答 默认 最新

  • dongmin4052 2016-02-09 14:35
    关注

    When you create a public directory in the Resources directory of your bundle, and run the assets:install --symlink command, a symbolic link is created in the web/bundles directory, corresponding to the name of your bundle.

    Also, to retrieve your image, you should use the relative path of the web directory instead of the absolute path of your asset, which is :

    /bundles/bundlename/images/background.jpg
    

    EDIT

    If you really doesn't want to place them inside a bundle, just move them from the app/Resources directly to the web folder, and call it in your css using :

    url('/images/background.jpg');
    

    See symfony2 how to access app/Resources/img from browser?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)