symfony – 找不到“GET /img/glyphicons-halflings.png”的路线

我有一个关于symfony2和bootstrap的问题.我不明白为什么我可以从环境“prod”加载图标图像,但不能从环境“dev”加载.在开发中,我收到此错误.

路线是“GET /img/glyphicons-halflings.png”.

Image web / img / glyphicons-halflings.png是../../vendor/twitter/bootstrap/img/glyphicons-halflings.png的符号链接

我得到这个错误

http://my.web.site/app_dev.php/img/glyphicons-halflings.png

并获得图像

http://my.web.site/img/glyphicons-halflings.png

UPDATE

我以这种方式包含bootstrap:

{% stylesheets '%kernel.root_dir%/../vendor/twitter/bootstrap/less/bootstrap.less' %}
    <link rel="stylesheet" type="text/css" href="{{ asset_url }}" />
{% endstylesheets %}

当我在prod使用时,这是有效的

<span class="icon-edit"></span>

我有这个资产配置:

assetic:
    debug:          %kernel.debug%
    use_controller: false
    bundles:        [ MyAwesomeBundle ]
    filters:
        lessphp:
            file: %kernel.root_dir%/../vendor/leafo/lessphp/lessc.inc.php
            apply_to: "\.less$"

我也试过创建我的收藏:

assetic:
    assets:
        senso:
            inputs:
                - '../img/glyphicons-halflings.png'
            outputs:
                - 'img/glyphicons-halflings.png'
            filters:
                - ?lessphp

:dump create web / assetic / senso.png和web / assetic / senso_glyphicons-halflings.png但是,…如何使用senso * .png图像?

解决方法

config.yml

assetic:
    use_controller: false
    filters:
         lessphp:
             file: "%kernel.root_dir%/../vendor/leafo/lessphp/lessc.inc.php"
    assets:   
        img_bootstrap_glyphicons_black:
            inputs:
                -  "%kernel.root_dir%/../vendor/twitter/bootstrap/img/glyphicons-halflings.png"
            output: "img/glyphicons-halflings.png"

        img_bootstrap_glyphicons_white:
            inputs:
                -  "%kernel.root_dir%/../vendor/twitter/bootstrap/img/glyphicons-halflings-white.png"
        output: "img/glyphicons-halflings-white.png"

        css_bootstrap:
            inputs:
                -  "%kernel.root_dir%/../vendor/twitter/bootstrap/less/bootstrap.less"
            output: "css/bootstrap.css"
            filters:
                - lessphp

        js_bootstrap:
            inputs:
                -  "%kernel.root_dir%/../vendor/twitter/bootstrap/js/*.js"
            output: "js/bootstrap.js"

config_dev.yml

assetic:
    use_controller: true

模板

{% stylesheets '@css_bootstrap' %}
<link href="{{ asset_url }}" rel="stylesheet" type="text/css"/>
{% endstylesheets %}

{% javascripts '@js_bootstrap' %}
<script src="{{ asset_url }}" type="text/javascript"></script>
{% endjavascripts %}

在运行assetic之后:dump –no-debug一切运行完美无缺.

基本信息

使用的cssrewrite过滤器,例如用于background-image:url(“..”)与@ Bundle语法不兼容.

{% stylesheets 
   '@AcmeYourBundle/Resources/public/css/your.css' 
   filter="cssrewrite" 
%}

不起作用.

此外,像url(“../ img / image.png”)这样的来源在开发环境中会失败而没有cssrewrite和assetic.use_controller:true – 可能的解决方法:使用url,如url(“/ img / image.png”)但不是使用子文件夹中的页面.

你应该记住……

<img src="img/image.png">

…将用于hostname / app_dev.php /以及除hostname /之外的所有其他路由.

解:

<img src="{{ asset('img/image.png') }}">

其他解决方案&提示

>当将img,css或js包含到您的页面时,请始终使用twig的asset(),{%stylesheets%}或{%javascripts%}函数.
>在样式表中使用相对网址,即url(“../ img / image.png”)
>尝试在config_dev.yml中设置assetic.use_controller:false并转储您的资产……如here所述
>在config.yml中创建asset collections,然后转储您的资产 – 请参阅我的answer here.
>使用cssembedded过滤器使用data-uris – 参考here将图像包含在css中
>将您的网络服务器配置为直接提供img / css / js文件夹中的文件,而不是通过symfony进行路由
>使用app_dev.php作为目录索引,为dev环境使用不同的主机名(即project.dev),从而生成没有/app_dev.php/的url

你可以在this question的答案中找到关于这个主题的一些很好的信息.

我个人工作流程:

>本地通配符dns服务器引入自动生成的url,如project.dev,project.prod,…(没有/app_dev.php/问题)
>在config.yml中创建asset collections(更清晰的模板,更多地控制资产)
> guard/grunt taskrunners而不是assetic:dump –watch(改变时重新转储,但测试和优化)
> source-maps和chrome-devtools而不是资产的资产:dump –debug(查看组合文件的真实源文件,less,sass,coffeescript,…)> tincr(直接从devtools保存)和livereload(立即查看更改)

相关文章

HTML代码中要想改变字体颜色,常常需要使用CSS样式表。CSS是...
HTML代码如何让字体盖住图片呢?需要使用CSS的position属性及...
HTML代码字体设置 在HTML中,我们可以使用标签来设置网页中的...
在网页设计中,HTML代码的字体和字号选择是非常重要的一个环...
HTML(Hypertext Markup Language,超文本标记语言)是一种用...
外链是指在一个网页中添加一个指向其他网站的链接,用户可以...