A
A
Alex1222016-11-21 20:10:15
Yii
Alex122, 2016-11-21 20:10:15

How to set image path from backend to frontend in yii2?

Hello! I am making a website. In the admin panel, the administrator uploads pictures so that they can be displayed on the frontend in the future. I am using advanced yii2 application. Pictures are saved at backend/web/files/subfolder_name/file_name.jpg. The layout is located at frontend/view/site/index (images should be displayed in it). Two virtual hosts are configured to "look" at the web folder on the front and backend, respectively. I tried different options to specify scr in the tag (and like this ./../../backend/web/next_path_to_image, and I created aliases like Yii::getAlias('@backend')). I googled, but I didn’t find a working version :( (they even suggested creating a symlink in yii2, I didn’t quite understand how it works, it might help). How can I specify the paths to the images that are on the backend in the layout (namely: ) or can something else try or remake the saving of pictures in another place? I hope I have explained the problem clearly.
Thanks everyone for the replies. My mistake was that I specified absolute paths (such as /opt/lammp/and_so_on, but I had to specify paths relative to the htdocs folder (because the default server looks at it) and set paths like http://localhost/and_path_to_my_folder_with_pictures /... I solved the problem in the following way: I specified in index.php (on the frontend), BACKEND_IMG=' http://localhost/and_path_to_my_folder_with_images_... ', and then I use . <img src="<?= BACKEND_IMG.'имя_картинки.jpg' ?>"

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Maxim Timofeev, 2016-11-22
@Alex122

1st mistake: images must be saved in the public folder of the frontend.
2nd error: You apparently need a url, not a path (path). Yii::getAlias('@backend') is just a path, not a url. The path is a location on the server that is not associated with a domain in any way and is used to save, delete, and modify files. And you need a url, relative to the domain (the root of the public folder).
not in yii, but on the server. To make it clearer, this is a shortcut, a virtual folder. That is, you will have a folder, for example img, in your public folder, which will somehow redirect requests to another location.

M
Mikhail Osher, 2016-11-21
@miraage


Or maybe try something else or remake the saving of pictures in another place?
It is logical that pictures should be loaded in common.

A
AlikDex, 2016-11-22
@AlikDex

The answer seems to lie on the surface. Set hard path aliases to the frontend in some bootstrap.php in the common config. In the same place, you can set an alias for the url with binding to the domain.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question