Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Symfony2 to load CSS, JS files directly and not via PHP?

Tags:

php

symfony

OLD QUESTION, SEE BELOW FOR THE UPDATED VERSION

My development environment is not the fastest. I takes roughly 500ms per PHP request. It's starting to become a problem with Symfony2 resource files because each of the resource files are being requested via Symfony's internal controllers:

http://localhost/myproj/app_dev.php/js/bb8690a_part_4_myJavaScriptFile_2.js

As can be seen, the files are loaded via the Symfony framework and not directly. Since I'm starting to have over 20 files to load, multiplying that with the 500ms makes page loads very slow. I want to load the files directly, but I am not sure how to do that.

This is part of the config.yml:

# Assetic Configuration
assetic:
    debug:          %kernel.debug%
    use_controller: false
    # java: /usr/bin/java
    filters:
        cssrewrite: ~

I thought setting use_controller to false would do it, but nope.

Is there a way to handle the loading of those resoures directly?

UPDATE:

This is the URL it tries to use now:

http://localhost/myproj/_controller/js/bb8690a_part_4_myJavaScriptFile_2.js

I have set use_controller to false for both dev and general configs. How do I get rid of that _controller part of the URL?

Edit: If I clear the cache, run assetic:dump and have use_controller as false, then upon reload I get Cannot load resource ".". I can't get around that problem unless I temporarily enable use_controller for one page load. After that, I disable it and reload and now it requests from that invalid URL that contains _controller.

It also seems to work in prod, but not in dev. Strange.

Template code:

{% stylesheets filter="cssrewrite"
            'bundles/outotecofil/css/reset.css'
            'bundles/outotecofil/css/*'

            output='css/dist/dist.css'
        %}
        <link rel="stylesheet" href="{{ asset_url }}" />
        {% endstylesheets %}

        {% javascripts
            '@OutotecCommonBundle/Resources/public/js/jquery-1.6.2.min.js'
            '@OutotecCommonBundle/Resources/public/js/jquery-ui-1.8.16.custom.min.js'
            '@OutotecCommonBundle/Resources/public/js/chosen.jquery.min.js'
            '@OutotecCommonBundle/Resources/public/js/widget/*'

            '@OutotecOFILBundle/Resources/public/js/OFILDependencyManager.js'
            '@OutotecOFILBundle/Resources/public/js/widget/*'
            '@OutotecOFILBundle/Resources/public/js/plant-scope.js'

            output='js/dist/dist.js'
        %}
        <script src="{{ asset_url }}"></script>
        {% endjavascripts %}

To be extremely clear: without app_dev.php (i.e. in prod mode), it works. Only in dev it does not and throws this "Cannot load resource "."" error unless I first enable use_controller for one request, after which I can disable it and reload though the URLs will then contain _controller/ in their paths.

like image 401
Tower Avatar asked Nov 24 '11 14:11

Tower


1 Answers

Try to remove this part of code in routing_dev.yml when use_controller is false :

_assetic:
    resource: .
    type:     assetic
like image 192
ArthurM Avatar answered Oct 03 '22 01:10

ArthurM