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

时间:2018-05-29 02:15:55

标签: php yii2 yii2-advanced-app

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

我尝试在DetailView::widget()

上执行此操作
...
[
    'attribute'=>'image',
    'value'=>('/frontend/web/uploads/'.$model->image),
    'format' => ['image',['width'=>'100','height'=>'100']],
],

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

3 个答案:

答案 0 :(得分:1)

是的,可以访问frontend/web/uploadsbackend视图。

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

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

答案 1 :(得分:1)

如果这是您从后端链接到前端的唯一位置,最简单的方法是在后端配置中定义别名:

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

并使用此别名构建文件URL:

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

答案 2 :(得分:-1)

        if (conn.State == ConnectionState.Open)
        {
            // you should always use parameterized queries to avoid SQL Injection
            cmd.Parameters.Add("@FName", OleDbType.VarChar).Value = fstName;
            cmd.Parameters.Add("@LName", OleDbType.VarChar).Value = lstName;
            cmd.Parameters.Add("@Address", OleDbType.VarChar).Value = adres;

            try
            {
                cmd.ExecuteNonQuery();
                MessageBox.Show(@"Data Added");
                conn.Close();
            }
            catch (OleDbException ex)
            {
                MessageBox.Show(ex.Source + "\n" + ex.Message);
                conn.Close();
            }
        }
        else
        {
            MessageBox.Show(@"Connection Failed");
        }