【问题标题】:Yii2-advanced, how to display images from backend in frontendYii2-advanced,如何在前端显示来自后端的图像
【发布时间】:2015-07-11 15:25:47
【问题描述】:

我的图片位于 /backend/web/uploads 中。 现在我想在 /frontend/views/site/index 中显示它们

所以在索引视图中,我试图像这样显示它们:

 $planet = Planet::find()->all();
foreach($planet AS $pl=> $p){
    echo Html::img('/backend/web/'.($p->path));
}   

在$p->路径-uploads/123.jpg

但此路径无效,我如何显示来自 /backend/web/uploads 在 /frontend/views/site/index 中?

【问题讨论】:

标签: php image frontend backend


【解决方案1】:

已经回复here

有两个主要选项可以实现这一目标。

1)您可以创建从frontend/web/imagesbackend/web/images 的别名,以便在后端从前端显示图像。

backend/web 文件夹运行ln -s ../../frontend/web/images images。最好先删除目标文件夹 (backend/web/images)。

2) 从此类目录发布图像的另一种方法是为该文件夹创建资产包,例如将图像复制到frontend/web/assets。您可以在official docs 中阅读有关资产包的更多信息。

【讨论】:

    【解决方案2】:

    转到您的frontend/config 并添加:

    'components' => [
        // You can name it whatever you want as it is custom
        'urlManagerBackend' => [
            'class' => 'yii\web\urlManager',
            'baseUrl' => 'yourbackendend/absolutepath/uploads/',
            'enablePrettyUrl' => true,
            'showScriptName' => false,
        ],
    ],
    

    那么你可以这样使用它:

    Html::img(Yii::$app->urlManagerBackend->baseUrl.'/uploads/your_img.png');
    

    【讨论】:

      【解决方案3】:

      我通过为后端图像和前端图像创建一个目录来做到这一点:

      在我的common/config/bootstrap.php中:

      Yii::setAlias('@root', dirname(dirname(__DIR__)));
      

      在我的common/config/main.php中:

      'modules' => [
            'yii2images' => [
               'class' => 'rico\yii2images\Module',
               //be sure, that permissions ok
               //if you cant avoid permission errors you have to create "images" folder in web root manually and set 777 permissions
               'imagesStorePath' => '@root/upload/store', //path to origin images
               'imagesCachePath' => '@root/upload/cache', //path to resized copies
               'graphicsLibrary' => 'GD', //but really its better to use 'Imagick'
               'placeHolderPath' => '@root/upload/store/no-image.png', // if you want to get placeholder when image not exists, string will be processed by Yii::getAlias
               'imageCompressionQuality' => 85, // Optional. Default value is 85.
            ],
      ]
      

      并在项目根目录下创建了“imagesStorePath”和“imagesCachePath”。

      【讨论】:

        猜你喜欢
        • 2017-12-21
        • 2016-04-09
        • 2020-05-14
        • 2021-10-26
        • 2021-08-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-24
        相关资源
        最近更新 更多