簡體   English   中英

如何使用Yii2 DetailView在后端渲染前端圖像?

[英]How to render an frontend image in backend using Yii2 DetailView?

我想在后端的視圖中渲染/顯示圖像,但是圖像位置在frontend/web/uploads

我嘗試在DetailView::widget()上執行此操作:

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

但是圖像沒有顯示。 有什么幫助嗎?

是的,可以訪問frontend/web/uploads backend視圖。

首先在backend->config->main.php添加urlManagerFrontend

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

然后像這樣訪問frontend/web/backend

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',
],

如果這是從后端鏈接到前端的唯一位置,則最簡單的方法是在后端配置中定義別名:

Yii::setAlias('@frontendUploads', 'https://repasca.devs/uploads');

並使用此別名來構建文件URL:

[
    'attribute' => 'image',
    'value' => Yii::getAlias('@frontendUploads') . '/' . $model->image,
    'format' => ['image', ['width' => '100', 'height' => '100']],
],
use yii\helpers\Html;  

 [
      'label' => 'Image',
      'format' => 'raw',
      'value' => Html::img('@web/uploaded_images/' . $model->image, ['width' => '10px', 'height' => '10px']),
 ],

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM