Yii2-advanced,如何在前端显示来自后端的图像 [英] Yii2-advanced, how to display images from backend in frontend
问题描述
我的图像在/ backend / web / uploads中。
现在我想在/ frontend / views / site / index中显示它们
I have my images in /backend/web/uploads. Now I would like to show them in /frontend/views/site/index
所以在索引视图中我试图像这样显示它们: / p>
So in index view I'm trying to show them like this:
$planet = Planet::find()->all();
foreach($planet AS $pl=> $p){
echo Html::img('/backend/web/'.($p->path));
}
in $ p-> path - uploads / 123.jpg
in $p->path - uploads/123.jpg
但是这条路径无效,如何在/ frontend / views / site / index中显示来自
/ backend / web / uploads的图片?
But this path is not valid, how can I display images from /backend/web/uploads in /frontend/views/site/index ?
推荐答案
已经回答这里。
实现这一目标有两个主要选择。
There are two main options to achieve that.
1)您可以从 frontend / web / images
创建别名到 backend / web / images
从前端显示后端的图像。
1) You can create alias from frontend/web/images
to backend/web/images
to display images in backend from frontend.
运行 ln -s ../../frontend/web/images images
来自 backend / web
文件夹。最好在删除之前删除目标文件夹( backend / web / images
)。
Run ln -s ../../frontend/web/images images
from backend/web
folder. It's better to delete destination folder (backend/web/images
) before doing that.
2) 从此类目录发布图像的其他方法是为该文件夹创建资产包,这样就可以将图像复制到 frontend / web / assets
中。您可以在官方文档中阅读有关资产包的更多信息。
2) Alternative way to publish images from such directory will be creating asset bundle for that folder, that way images will be copied in frontend/web/assets
for example. You can read more about asset bundles in official docs.
这篇关于Yii2-advanced,如何在前端显示来自后端的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!