How to display images in symfony

Asked

Viewed 137 times

0

I’m starting at symfony and would like to know the correct way to display an image.
I’m changing the index.html.twing which is in app/Resource/view/default/ and base.html.twing which is in app/Resource/view/.
I put <img src="../../../src/AppBundle/Resources/public/imagens/logo.png" />

<img src="{{ asset('imagens/dev-total-logo.png') }}" />

How is the right way to do this?

1 answer

1


The correct form is the second:

<img src="{{ asset('imagens/dev-total-logo.png') }}" />

There are several reasons for this. The first is that this way your application can display the image regardless of where your static file folder is, as long as Symfony knows where it is.

Also, images (as well as any other static file such as a CSS file or a JS file) must be in the folder web/. If your static files are inside a Bundle, that is, inaccessible, it is necessary to make use of the command assets:install for the files to be copied (or linked) in the public folder.

At Symfony 2.x:

app/console assets:install 

At Symfony 3.x:

bin/console assets:install
  • I was putting the files in src/Appbundle/Resources/public/images/. I moved to the Web folder and it worked.

  • It is not necessary to move, you can leave the files there if you want, and only use the above command to publish them. : ) The advantage is that you keep the static files separated by Bundle, without having to mix everything up.

  • I used more gave error.

Browser other questions tagged

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