Symfony Twig - 图像没有显示

Little bit confused here. I have looked at a few threads and they are not solving it.

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

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

Edit:

2) Works
1) does not.

The crazy thing is, in PHPstorm, if I do /web in the src route it selects it for me, then it selects uploads for me and can even see the file. It just wont display!?

The below works for me, it displays the image. So there is no real rush on this now. However it is interesting to why 1) doesn't work?

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

The answer should be easy to find by searching the docs.

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

doesn't work, simply because, if this code you've wrote it inside, let's say, AppBundle/Resources/views/Default/index.html.twig file, then this means the web/uploads/ directory structure should be present inside this directory (AppBundle/Resources/views/Default/) - and having the image inside it.

Which is not your case. So for AppBundle/Resources/views/Default/index.html.twig, the src should be:

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

That's why the asset() function is useful. Because it maps the path to your public directory (declared in composer.json file, under the symfony-web-dir key); so, no matter from where you are calling this function, it will always return the path to the public directory, so you don't have to worry about all those ../../ inclusions.

You have in your code:

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

But probably you domain is set on folder project mySite/web that shouldn't not correct from browser. Then you final link, should be

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

or

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

If this won't help, then maybe someone more experienced could help.

if 2. works it should be:

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

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

But that means your vhost root is not set correctly is should point to /web but it seems it points to the parent of /web which is the root of your Symfony project.

web/ is a basic directory web files, and here you start search you front-ent file: [img, js, css]