doudu9094
2018-05-29 02:15
浏览 175
已采纳

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

I want to render/show an image in a view on backend, but the image location is in frontend/web/uploads.

I tried doing this on the DetailView::widget():

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

But the image is not showing up. Any help?

图片转代码服务由CSDN问答提供 功能建议

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

我尝试在 DetailView :: widget()上执行此操作:

  ... 
 [
'attribute'=>'image',
'value'=>('/  frontend / web / uploads /'.$ model-> image),
'format'=>  [ '图像',[ '宽度'=> '100', '身高'=> '100'],
]的,
   
 
 

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

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • drl57959 2018-05-29 09:57
    已采纳

    If this is the only place where you're linking to frontend from backend, the easiest way would be defining alias in your backend config:

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

    And use this alias for building file URL:

    [
        'attribute' => 'image',
        'value' => Yii::getAlias('@frontendUploads') . '/' . $model->image,
        'format' => ['image', ['width' => '100', 'height' => '100']],
    ],
    
    打赏 评论
  • dongtangyi8962 2018-05-29 04:40
    use yii\helpers\Html;  
    
     [
          'label' => 'Image',
          'format' => 'raw',
          'value' => Html::img('@web/uploaded_images/' . $model->image, ['width' => '10px', 'height' => '10px']),
     ],
    
    打赏 评论
  • dongxieyou3314 2018-05-29 05:57

    Yes it is possible to access frontend/web/uploads to backend view.

    First add urlManagerFrontend in backend->config->main.php like

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

    And then access the frontend/web/ to the backend like

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

    In DetailView::widget like

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

相关推荐 更多相似问题