Image in gridview in yii2

前端 未结 9 1844
梦毁少年i
梦毁少年i 2020-12-16 04:34

How to put an image in GridView in yii2?I have the following code. But its not displaying the image as it is not giving any image url. Where to put the image url?

         


        
9条回答
  •  没有蜡笔的小新
    2020-12-16 05:19

    Yii 2 has built-in helper for building urls. You can bulld the url to image by path too (by passing second parameter $scheme).

    So I recommend using this:

    GridView:

    use yii\helpers\Url;
    
    [
        'format' => 'image',
        'value' => function ($model) {
            return $model->getImageUrl(); 
        },
    ],
    

    Model:

    public function getImageUrl()
    {
        return Url::to('@web/path/to/logo/' . $this->logo, true);
    }
    

提交回复
热议问题