Asset function does not work on Symfony 5

Asked

Viewed 47 times

0

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

After configuring my Controller and routes, I started coding the Twig template using Bootstrap 4 via CND 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 function asset() did not return the path of files stored in "public folder".

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

I checked my "Composer.json" file and Asset is configured in the following version: "symfony/asset": "5.1.*". The same is correctly installed in the folder "vendor".

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

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

I retraced the installation steps of the "Asset" component following the instructions of the documentation below and also did not obtain result:

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 to it.

Grateful.

1 answer

0

I found the solution to that question by chance:

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

Within public/, executed the following command:

$php -S localhost:8000

Generally speaking, the system built on Symfony worked normally, minus the method asset() (only realized this one flaw).

As a solution, simply access the project’s main directory and run the server using the following command:

$ php -S localhost:8000 -t public/

I would like someone to complement the answer with theoretical information about how this corrects the failure, as I am beginner and my knowledge about servers is limited.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.