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 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)