【问题标题】:image are not shown in admin in yii 2.0yii 2.0 中的 admin 中未显示图像
【发布时间】:2016-03-14 09:11:19
【问题描述】:

我想在管理面板中显示图像而不是图像名称。但我在 yii 1.1 中有很多例子,但不理解在 yii 2.0 中使用。 这是我的网格视图

   <?php

use yii\helpers\Html;
use yii\grid\GridView;
use yii\imagine\Image;

/* @var $this yii\web\View */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = Yii::t('app', 'Contents');
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="content-index">

    <h1><?= Html::encode($this->title) ?></h1>

    <p>
        <?= Html::a(Yii::t('app', 'Create Content'), ['create'], ['class' => 'btn btn-success']) ?>
    </p>

    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],

            'id',
            'label',
            'title',
            'content',
        /*  ['attribute' => 'main_image',
                'header' => 'Image',
                'value' => function( $data ) {

                return ?><img src="<?= Yii::$app->request->baseUrl ?>/upload/content/<?php echo $data->main_image?>">
            <?php
           }
           ],*/

                [
                'attribute'=>'photo',
                'value' =>  Html::a(Html::img(Yii::getAlias('@web').'/upload/content'.$content->main_image, ['alt'=>'some', 'class'=>'thing', 'height'=>'100px', 'width'=>'100px']), ['site/zoom']),
                'format' => ['raw'],
                ],



            ['class' => 'yii\grid\ActionColumn'],
        ],
    ]); ?>

</div>

我收到未定义内容的错误,我还尝试了上面的注释代码。但它在归档列表中显示“未设置”。

如果有任何想法请帮忙...

【问题讨论】:

    标签: php yii2


    【解决方案1】:

    你应该这样格式化你的属性(希望路径是正确的)

           ['attribute' => 'main_image',
                'header' => 'Image',
                'format' => 'raw',
                'value' => function( $model ) {
                    return '<img src="' . echo Yii::$app->request->baseUrl . '/upload/content/'. $model->main_image .'">' ;
           }
        ],
    

    【讨论】:

    • 请 .. 告诉我是否有错误或显示什么 .. .. 用 var_dump 检查函数结果并调整路径 ..
    • 先生,我得到了解决方案,但我可以使用相同的代码在 view.php 文件中显示图像吗?
    • @vikash。视图需要不同的风格。参考:stackoverflow.com/questions/26896529/…
    【解决方案2】:

    我使用此代码得到解决方案

       ['attribute' => 'main_image',
                        'header' => 'Image',
                        'format' => ['raw'],
                        'value' => function( $data ) {
    
                        return Html::img(Yii::getAlias('@web').'/upload/content/'.$data->main_image, ['alt'=>'some', 'class'=>'thing', 'height'=>'100px', 'width'=>'100px']);
    
                   }
                   ],
    

    【讨论】:

      猜你喜欢
      • 2013-06-08
      • 1970-01-01
      • 1970-01-01
      • 2014-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-30
      • 1970-01-01
      相关资源
      最近更新 更多