首页 > 解决方案 > 如何使用 Yii2 DetailView 在后端渲染前端图像?

问题描述

我想在后端的视图中渲染/显示图像,但图像位置在frontend/web/uploads.

我试过这样做DetailView::widget()

...
[
    'attribute'=>'image',
    'value'=>('/frontend/web/uploads/'.$model->image),
    'format' => ['image',['width'=>'100','height'=>'100']],
],

但是图像没有显示出来。有什么帮助吗?

标签: phpyii2yii2-advanced-app

解决方案


是的,可以访问frontend/web/uploads查看backend

先加urlManagerFrontendbackend->config->main.phplike

'components' => [
    'urlManagerFrontend' => [
        'class' => 'yii\web\urlManager',
        'baseUrl' => 'frontend/web/', //Access frontend web url
    ],
],

然后访问frontend/web/backendlike

Yii::$app->urlManagerFrontend->baseUrl.'/uploads/your_image.jpg'

在 DetailView::widget 之类的

[
   'attribute'=>'image',
   'value'=> function ($model) {
        return Html::img(Yii::$app->urlManagerFrontend->baseUrl.'/uploads/'.$model->image, ['alt' => 'No Image', 'width' => '10px', 'height' => '10px']);
   },
   'format' => 'raw',
],

推荐阅读