0
votes

Symfony looks to be looking for assets in a relative location based on the url path I navigate to. The assets load and are applied correctly when I navigate to a "first-level" path such as example.com/mpre, example.com/test, example.com/foo, but then all of my assets 404 when I navigate to a "second-level" url such as example.com/mpre/test, example.com/test/foo, example.com/foo/bar. Why is this? Is there a way to get the framework to look in one spot for the assets regardless of the url?

I have 2 urls

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

My assets (css, js) load fine on the first url, example.com/mpre, but then all of them 404 when I navigate to the second url, example.com/mpre/test

When I go into the inspector on each page to see the path to the assets, I see the following:

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

In config.yml I have the following line for assetic:

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

Additional Information

  • I am not using the cssrewrite filter

Edit 1

  • I installed Symfony 2.3 via composer
  • Im including the css in base.html.twig and using assetic to compile them

    {% 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'
    %}
    
  • The paths are standard routes. Each path corresponds to a controller action which renders a view

  • Here is what the view looks like when the assets 404 enter image description here

  • Here is the dump of ./app/console config:dump-reference assetic

Default configuration for extension with alias: "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 variables:

    # 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:                 []

Edit 2

I've found the source of the problem. The link path was relative, so I changed it to use the baseUrl. <script src="{{ app.request.baseUrl }}/bundles/app{{ asset_url }}"></script> and everything works great.

1
How did you install Symfony? Did you do it over Composer? If so, what package/version did you install? I've never had to manually change the write_to parameter. What does your view look like? How are you including the css? and are the paths you have above setup as standard routes on a Controller? or something different?nateevans
What is the output of ./app/console config:debug assetic? I'm assuming there is an issue between the write_to and read_from settings. See symfony.com/doc/current/reference/configuration/assetic.htmlnateevans
I've added the requested information in the original questionPatrick

1 Answers

0
votes

I'm assuming there is an issue with your the write_to setting.

Try these settings and see what happens.

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

These should be the default assetic config settings for read_from and write_to.

Your settings show %kernel.root_dir%/../web/bundles/app/. Assetic will generate the bundles folders automatically, so setting up your own bundles folder is probably causing unexpected results.

In bundles/, app corresponds to your bundle AppBundle. A bundle named FooBundle would have it's assets in web/bundles/foo/ etc.

Make sure you clear your Symfony cache after making changes to the config file.