Including Assets (CSS, JS) in Symfony 4 / Twig Template

Paths in the asset function should be relative to your public directory, not the project root.

So, pretending your server's docroot is public_html, and an asset is in public_html/css/main.css, the asset call would be {{ asset('css/main.css') }}.

Additional note: The asset function doesn't give you the ability to put assets outside the public directory. They must be in the public directory.


Firstly maybe you have to add the asset functionality to Symfony:

composer require symfony/asset

Then you should to use in Twig temlate the relative path to your public directory:

<link href="{{ asset('css/style.css') }}" rel="stylesheet" />

for the public/css/style.css style file.

BTW it is also works for Symfony 5.