Search code examples
symfonyassetssymfony-3.4

Asset returns error 500 for a stylesheet with Symfony3


I'm encounting a problem with assets. My problem seems simple but I can't find a solution. I have search Google and other earch engines, as well as StackOverflox, Github.

I just want to use the asset function in twig templates. The code is very simmple:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8" />
        <title>{% block title %}Welcome!{% endblock %}</title>
        {% block stylesheets %}{% endblock %}
        <link rel="stylesheet" href="{{ asset('css/bootstrap.min.css') }}" />
        <link rel="icon" type="image/x-icon" href="{{ asset('favicon.ico') }}" />
    </head>
    <body>
        {{ knp_menu_render('AppBundle:MenuBuilder:mainMenu') }}
        {% block body %}{% endblock %}
        {% block javascripts %}{% endblock %}
    </body>
</html>

I just want to add Bootstrap stylesheet in my app. But when I refresh my page, I get an 500 Internal Server Error for the stylesheet. The rest of the page shows up but it can't retrieve the sheet. The URL requested is

http://arthur-dev.lyon-bercy.ratp:84/css/bootstrap.min.css

My development domain is http://arthur-dev.lyon-bercy.ratp:84/. So nothing strange in the requested URL. But shouldn't there be a reference to app_dev.php? And when I manually request http://arthur-dev.lyon-bercy.ratp:84/app_dev.php/css/bootstrap.min.css, the Symfony Framework shouts a

"No route found for "GET /css/bootstrap.min.css"

I have cleared the cache, installed the assets with absolute path or relative path, symlinked or hard linked. Nothing resolved my issue.

I'm using Symfony3.4 with Nginx and PHP FPM 7.2 under docker. Here are the exact configuration files: config.yml: https://framabin.org/?771da57e9f933f5c#A9RmC/Ld0y8k7SZ5EoWCBkcjVF/VWHRO6+tT7d2e3/s= composer.json: https://framabin.org/?0a3e9ae8ec9ff32c#gmg7lnD3fAkAlBkX408yn8Jr4buuBEt1GosqvGggvm8=
composer.lock: https://framabin.org/?55a3c320d4e6ee9a#YB0fyupuUJL5SGYeZyJI/jGOf53HTd/xudExwCr9+N8=
docker-compose.yml: https://framabin.org/?0583b288a0b7c071#Z9ATFv8QYmfkTidC8F1xehzfM3SjOtW+rRgop9t6ddU=
PHP dockerfile: https://framabin.org/?dd514d9548b1545b#EEmexWzgNzE2W6ODktu5iB3QWATJGz+xZV7B73Fy2gI=
nginx dockerfile: https://framabin.org/?160536a41c6d857c#iK6w74WKm3Q2OKWSP/j6EPdePvc5avxqgqqX74VF91o=

For Information, I tried with AsseticBundle but I get an issue too :

An exception has been thrown during the rendering of a template ("Unable to generate a URL for the named route "_assetic_3d14251_0" as such route does not exist.").

Any help is the most welcome

EDIT: Added link to config.yml file


Solution

  • I have found the solution for my problem. I give here the answer.
    As I mentionned in my message I use Docker to create my environment. It is based on Maxpou's Symfony with Docker project (https://github.com/maxpou/docker-symfony).

    The project isn't up-to-date with the Symfony 3.4. So there are some correction to do. I will make a PR later but here for information the solution: just add the following line in docker-compose.yml at the nginx level:

        volumes:
            - ${SYMFONY_APP_PATH}:/var/www/symfony
    

    So now it looks like that:

    nginx:
        build: nginx
        ports:
            - 84:80
        volumes:
            - ${SYMFONY_APP_PATH}:/var/www/symfony
    

    The issue was that Nginx didn't have access to the directory with the assets so it returned a 404 page. Now, the file is available directly in Nginx.