Yii 在 CDetailView 中显示图像



我已经将图像存储在我的Yii项目的图像文件夹(根)中。我想在受保护/查看/学院中的 CDetailView 中显示图像

我的代码

array(
            'label'=>'CollegeLogo',
            'type'=>'raw',
            'value'=>CHtml::image("../".Yii::app()->baseUrl."/Images/".$model->CollegeLogo),
        ),

它不起作用

<?php 
$this->widget('zii.widgets.CDetailView', array(
    'data'=>$model,
    'attributes'=>array(
        'title',
        'date',
        'description',
         array(
            'type' => 'raw',
            'value'=>CHtml::image(Yii::app()->baseUrl."/upload/".$model->image,'alt',array("width"=>"50px" ,"height"=>"50px"))
            ),
        'time',
        'user_id',
    ),
)); ?>

删除代码中的"../"。Baseurl 将从根文件夹返回路径。

'value'=>CHtml::image(Yii::app()->baseUrl."/Images/".$model->CollegeLogo),

试试这个

'value'=>CHtml::image(Yii::app()->getBaseUrl(true) . '/Images/'.$model->CollegeLogo),

最简单的方法是将字符串格式"Name:Type:Label"与图像类型一起使用

<?php
'attributes' => array(
  "CollegeLogo:image",
  //...
),

如果 CollegeLogo 属性不返回绝对 url,我会在返回它的模型中创建一个虚拟属性:

public function getCollegeLogoUrl() {
    return "../".Yii::app()->baseUrl."/Images/".$model->CollegeLogo;
}

然后在小部件中使用它:

<?php
'attributes' => array(
  "collegeLogoUrl:image",
  //...
),

这段代码有效

array(
            'label'=>'CollegeLogo',
            'type'=>'raw',
            'value'=>CHtml::tag('img',
                array("title"=>"CollegeLogo",
                "src"=>Yii::app()->baseUrl."/CollegeImages/".$model->CollegeLogo,
                "style"=>"height:70px")
            )
        ),