Yii2 : Show image from Folder Common

2019-01-28 21:47发布

I have a picture in folder common that i want to show in view, but it doesn't shown.

My code is :

<img src="<?= Yii::$app->request->baseUrl . '/common/web/img/ava.jpg' ?>" class=" img-responsive" >

It's generated to be <img src="/simdsm/common/web/ava.jpg" class=" img-responsive"> but the image does not appear.

How to fix it?

标签: image yii2
2条回答
家丑人穷心不美
2楼-- · 2019-01-28 22:31

We can do it easily by simple step of below In common/config/params-local.php

Yii::setAlias('@imageurl', 'http://localhost/project_name/backend/web/');

in above example i have my image under backend/web/images/product_images

In frontend, i am getting like,

<img src="<?php echo Yii ::getAlias('@imageurl'); ?>/images/product_images/<?= $product['image']?>" alt=" " class="img-responsive" />

it's simply working for me...

查看更多
三岁会撩人
3楼-- · 2019-01-28 22:49

In case of using advanced application template, images should be placed in these web accessible folders - frontend/web or backend/web.

Also usually I create alias from frontend/web/images to backend/web/images to display images in backend from frontend.

What you mentioned is not web accessible directory.

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.

查看更多
登录 后发表回答