简体   繁体   中英

Symfony asset not including stylesheets

I have symfony 4.1 installed via composer and the asset component.

I have a css file at assets/css/dashboard.css and in my templates/base.html.twig I included this:

    {% block stylesheets %}
        <link href="{{ asset('css/dashboard.css') }}" rel="stylesheet" />
    {% endblock %}

I haven't modified anything, but somehow the template is not getting called.

I tried many variations of the path, adding slashes, dots thinking maybe the path is wrong but nothing.

The css file has no issue, I dumped it's contents and pasted it inside my template's <style></style> tags and it works.

I don't know what is going on

For short:

The asset() function points on your public folder.

So in your example, your dashboard.css should be in public/css/dashboard.css than this <link href="{{ asset('css/dashboard.css') }}" rel="stylesheet" /> should work.

The longer explanation:

Usually you will structure your scripts, styles and images in your assets folder. But in production you don´t need good readable css/less/scss/js code and so you want to minify it. And the minified (uglified) code should be copied to your public folder.

So you want to use Webpack Encore to minify your code and "deploy" it to your projects public folder.

In symfony´s documentation you can find a simple Example how to use Webpack Encore .

Why the assets are going to the public folder?

During the security concept of symfony (and the most other frameworks) the public folder is the only accessible folder. So everything the browser have to read goes there. In case of symfony your styles, scripts, images and so on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM