首页 > 解决方案 > 无法使用 yii2 在滑块中显示图像

问题描述

我有代码来显示这样的视图

<div class="col-md-10">
        <div class="col-md-4">
            <?php

            $items = [];

            foreach ($team->gallery as $item){
                $items[] = [
                        'content' => '<img style="width:300px;" src="'.$item->filepath.'"/>',
                        'caption' => '<h4>'.$item->name.'</h4><p>'.$item->description.'</p>',
                    ];
            }

            echo Carousel::widget([
                'items' => $items,
            ]);

            ?>
        </div>

和像这样获取画廊的模型

public function getLeague(){
        return $this->hasOne(Leagues::className(), ['id' => 'league_id']);
    }

    public function getGallery(){
        return $this->hasMany(TeamGalleries::className(), ['team_id' => 'id']);
    }

但是我无法在我的数据库中获取文件路径,因此我无法在我的网站上显示我的图片,如何解决这个问题?

我使用 yii2,我的图片上传的文件夹在 yii2 basic 的 web 文件夹中

标签: imageyii2show

解决方案


如果您的图像在 web 文件夹中,您可以轻松使用Yii 别名,该文件夹有一个条目:@web.

// Replace this

'<img style="width:300px;" src="'.$item->filepath.'"/>'

// with

'<img style="width:300px;" src="' . Yii::getAlias('@web') . '/' . $item->filepath . '"/>'

指南中有关别名的更多信息。


推荐阅读