在html img标签中显示后端前端的图像

时间:2016-05-21 06:55:08

标签: yii2 yii2-advanced-app yii-url-manager

我想在后端显示一个图片,上传到前端/网络/上传的目录

这是我的后端配置:

'urlManagerFrontEnd' => [
        'class' => 'yii\web\urlManager',
        'baseUrl' => 'shop/frontend/web',
        'enablePrettyUrl' => true,
        'showScriptName' => false,
    ],

这是我在视图文件中的Html img标记

Html::img(\Yii::$app->urlManagerFrontEnd->baseUrl.'/' .$pathImg, ['class' => 'file-preview-image']);

但它会返回此网址:

http://localhost/shop/backend/web/product/shop/frontend/web/uploads/My Image.jpg

我的代码有什么问题?

1 个答案:

答案 0 :(得分:0)

shope移除'baseUrl' => 'shop/frontend/web',如果不起作用,请尝试以下解决方案,基本网址应如下所示:'baseUrl' => 'frontend/web/',

试试这个!

后端配置:

'components' => [
        //can name it whatever you want as it is custom
        'urlManagerFrontend' => [
                'class' => 'yii\web\urlManager',
                'baseUrl' => 'frontend/web/',
                'enablePrettyUrl' => true,
                'showScriptName' => false,
        ],
],

前端配置(可选)

'components' => [
        'urlManagerBackend' => [
                'class' => 'yii\web\urlManager',
                'baseUrl' => 'backend/web/',
                'enablePrettyUrl' => true,
                'showScriptName' => false,
        ],
],

在img标签中:

Html::img(Yii::$app->urlManagerFrontend->baseUrl.'/uploads/My Image.jpg');