首页 文章

Symfony正在根据网址改变寻找资产的位置

提问于
浏览
0

Symfony希望根据我导航到的url路径在相对位置查找资产 . 当我导航到 example.com/mpreexample.com/testexample.com/foo 之类的"first-level"路径时,资源加载并正确应用,但是当我导航到 example.com/mpre/testexample.com/test/fooexample.com/foo/bar 等"second-level"网址时,我的所有资产都是404 . 为什么是这样?有没有办法让框架在资源的一个位置查看,而不管网址是什么?

我有2个网址

example.com/mpre
example.com/mpre/test

我的资产(css,js)在第一个网址 example.com/mpre 上加载正常,但是当我导航到第二个网址时,所有这些资产都是404, example.com/mpre/test

当我进入每个页面的检查器以查看资产的路径时,我看到以下内容:

example.com/bundles/app/css/023f7f6_style.css_4.css       //example.com/mpre   200 OK response
example.com/mpre/bundles/app/css/c8b625f_style.css_3.css     //example.com/mpre/test   404 Not Found response

config.yml 中我有以下资产:

assetic:
    write_to:   %kernel.root_dir%/../web/bundles/app/

其他信息

  • 我是 not 使用 cssrewrite 过滤器

编辑1

  • 我通过composer安装了Symfony 2.3

  • 我在base.html.twig中包含css并使用assetic来编译它们

{% stylesheets
  '@AppBundle/Resources/assets/css/bootstrap_loader.css.scss'
  '@AppBundle/Resources/assets/css/stately/*'
  '@AppBundle/Resources/assets/css/bootstrapValidator.min.css'
  '@AppBundle/Resources/assets/css/style.css.scss'
  '@AppBundle/Resources/assets/css/learners.css'
%}
  • 路径是标准路线 . 每个路径对应于呈现视图的控制器动作

  • 以下是资产404
    enter image description here
    时的视图

  • 这是 ./app/console config:dump-reference assetic 的转储

带别名的扩展的默认配置:“assetic”

assetic:debug:%kernel.debug%use_controller:enabled:%kernel.debug%profiler:false read_from:%kernel.root_dir%/ .. / web write_to:%assetic.read_from%java:/ usr / bin / java node: / usr / bin / node node_paths:[] ruby:/home/ubuntu/.rvm/rubies/ruby-2.0.0-p195/bin/ruby sass:/home/ubuntu/.rvm/gems/ruby-2.0.0 -p195 / bin / sass变量:

# Prototype
    name:                 []
bundles:

    # Defaults:
    - FrameworkBundle
    - SecurityBundle
    - TwigBundle
    - MonologBundle
    - SwiftmailerBundle
    - AsseticBundle
    - DoctrineBundle
    - SensioFrameworkExtraBundle
    - AppBundle
    - LswMemcacheBundle
    - WebProfilerBundle
    - SensioDistributionBundle
    - SensioGeneratorBundle
assets:

    # Prototype
    name:
        inputs:               []
        filters:              []
        options:

            # Prototype
            name:                 []
filters:

    # Prototype
    name:                 []
workers:
    cache_busting:
        enabled:              false
twig:
    functions:

        # Prototype
        name:                 []

编辑2

我找到了问题的根源 . 链接路径是相对的,所以我将其更改为使用baseUrl . <script src="{{ app.request.baseUrl }}/bundles/app{{ asset_url }}"></script> 一切都很好 .

1 回答

  • 0

    我假设您的 write_to 设置存在问题 .

    尝试这些设置,看看会发生什么 .

    assetic:
        read_from: '%kernel.root_dir%/../web'
        write_to:  '%assetic.read_from%'
    

    这些应该是 read_fromwrite_to 的默认资产配置设置 .

    您的设置显示 %kernel.root_dir%/../web/bundles/app/ . Assetic会自动生成捆绑包文件夹,因此设置自己的 bundles 文件夹可能会导致意外结果 .

    bundles/ 中, app 对应于您的包 AppBundle . 名为 FooBundle 的包将在 web/bundles/foo/ 等中拥有它的资产 .

    确保在更改配置文件后清除Symfony缓存 .

相关问题