Why doesn't the asset function work in Symfony 5?

Viewed 1769

I installed Symfony 5 using the $ composer create-project symfony/website-skeleton my_project_name command with the intention of getting the most complete Symfony package possible.

After configuring my Controller and the routes, I started coding the Twig template using Bootstrap 4 via CDN and the page rendering worked normally.

When I decided to configure the Bootstrap files in my location and no longer via CDN, I realized that the asset() function did not return the path of the files stored in the "public" folder.

The same occurs with other files and directories located inside the "public" folder.

I checked my "composer.json" file and the asset is configured in the following version:

"symfony/asset": "5.1.*"

It is correctly installed in the "vendor" folder.

Here is an example of using the function in my code in the "base.html.twig" file:

href="{{ asset("bootstrap/css/bootstrap.css") }}

I redid the steps for installing the "asset" component following the instructions in the documentation below and I also did not get any results:

https://symfony.com/doc/current/components/asset.html

I would like to know if anyone has been through it and if they have any solution for it.

Thankful.

3 Answers

I found the solution to this issue by chance:

The error was related to the way I ran the PHP server. I was running directly from the public/ directory.

Within public/, execute the following command:

$ php -S localhost:8000

In general, the system built in Symfony worked normally, except the asset() method (I only noticed this single flaw).

As a solution, it was enough to access the main project directory and run the server using the following command:

$ php -S localhost:8000 -t public/

I would like someone to supplement the answer with theoretical information on how this corrects the flaw, as I'm a beginner and my knowledge of servers is limited.

For those who are using Symfony 6 I got the same problem to fix it i just go to the config/packages/webpack_encore.yaml

framework:
assets:
    json_manifest_path: '%kernel.project_dir%/public/build/manifest.json'

replace it by

framework:
assets:
    version: 'v2'
Related