eniac05
eniac05

Reputation: 491

How to render an frontend image in backend using 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?

Upvotes: 0

Views: 1092

Answers (2)

rob006
rob006

Reputation: 22174

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

Upvotes: 1

vishuB
vishuB

Reputation: 4261

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

Upvotes: 2

Related Questions