Symfony Twig - image not showing

Confuse a little here. I have looked at several threads and they do not solve it.

1)  <img src="/web/uploads/ce438b2eb1c01d85f3d6d6c52efc1175.png"/>

2)   <img src="{{ asset('/uploads/ce438b2eb1c01d85f3d6d6c52efc1175.png')}}"/>

      

Edit:

2) Works
1).

The crazy thing, in PHPstorm, if I do / web in the src route, it picks it for me, then it picks downloads for me and can even see the file. Is it just not showing up !?

Below works for me an image that displays an image. So there is no real rush now. However, I wonder why 1) doesn't work?

  <img src="{{ asset('/uploads/' ~ media)}}"/>

      

+3


source to share


4 answers


The answer can be easily found by searching for documents.

<img src="/web/uploads/ce438b2eb1c01d85f3d6d6c52efc1175.png"/>

      

does not work, simply because if you wrote this code inside, say, AppBundle/Resources/views/Default/index.html.twig

file, then this means that the directory structure web/uploads/

must be present inside this directory ( AppBundle/Resources/views/Default/

) - and have an image inside it.



This is none of your business. So, for AppBundle/Resources/views/Default/index.html.twig

, it src

should be:

<img src="../../../../../uploads/ce438b2eb1c01d85f3d6d6c52efc1175.png

      

This is why the function asset()

is useful. Because it matches the path to your public directory (declared in the file composer.json

under the key symfony-web-dir

); so no matter where you call this function, it will always return the path to the public directory, so you don't have to worry about all those inclusions ../../

.

0


source


You have the code:

<img src = "mySite / web / uploads / {{media}}" / ">



But your domain is probably set to a folder project mySite/web

that shouldn't be patched from the browser. Then you are the final link, should be

<img src = "mySite / web / uploads / {{media}}" / ">



or

<img src = "{{asset ('/ mySite / web / uploads / {{media}}')}}" / ">

If that doesn't work, maybe someone more experienced can help.

0


source


if 2. works it should be:

  • <img src="/uploads/ce438b2eb1c01d85f3d6d6c52efc1175.png"/>

  • <img src="{{ asset('/uploads/ce438b2eb1c01d85f3d6d6c52efc1175.png')}}"/>

But that means your root host is not installed correctly. It should point to /web

, but it seems to point to the parent /web

which is the root of your Symfony project.

0


source


web/

are the base web directory files and this is where you start looking for your front file: [img, js, css]

0


source







All Articles