簡體   English   中英

Yii2:URL 重寫后無法在后端和前端顯示圖像

[英]Yii2: Unable to show image on backend and frontend after URL Rewriting

我已經更改了前端/網絡和后端/網絡 url,我正在應用程序根目錄中的上傳文件夾中上傳文件,例如“/yii2app/uploads”,但無法顯示圖像。

使用 htaccess 更改前端和后端 url。 所有鏈接都工作正常。 另外,我正在上傳根文件夾中的文件。 從后端應用程序,我將圖像上傳到根文件夾中,這很好。 但我無法查看圖像。

我的根文件夾 HTACCESS

Options -Indexes

IndexIgnore */*

#follow symbolic links
Options FollowSymlinks
RewriteEngine on
#without trailing slash
RewriteRule ^admin(/.+)?$ backend/web/$1 [L,PT]
#with trailing slash
RewriteRule ^admin/(.+)?$ backend/web/$1 [L,PT]
RewriteRule ^(.+)?$ frontend/web/$1

前端 HTACCESS

RewriteEngine on
RewriteBase /yii2app

# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# otherwise forward it to index.php/controller/action
RewriteRule (?!web\/) index.php/$1

后端HTACCESS

RewriteEngine on
RewriteBase /yii2app/admin

# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# otherwise forward it to index.php
RewriteRule . index.php

在 config/main.php 中,添加了 home 和 base url,

/yii2app - frontend
/yii2app/admin - backend

另外,我在 common/config/bootstrap.php 中創建了別名“@root”

Yii::setAlias('@root', realpath(dirname(__FILE__).'/../../'));

問題是當我嘗試加載 img 時,它沒有顯示。 我嘗試使用此解決方案在根文件夾中上傳圖像。 請幫忙。

更新

下面的代碼我用來查看gridview中的圖像(從后端),但沒有用

[
                'label'=>'Event Image',
                'attribute'=>'image_url',
                'format'=>'html',
                'value'=>function($model){
                    return Html::img(Yii::getAlias('@root')."/uploads/events/".$model->id."/".$model->image_url);
                }
            ],

此外,從后端應用程序嘗試

[
                'label'=>'Event Image',
                'attribute'=>'image_url',
                'format'=>'html',
                'value'=>function($model){
                    return Html::img("../../uploads/events/".$model->id."/".$model->image_url, ['width' => '170px']);
                }
            ],

我犯了一個錯誤嗎?

您沒有為根目錄設置別名,但您應該使用 web 目錄中的符號鏈接並查看圖像。

frontendbackendweb目錄中創建一個符號鏈接。

對於前端

ln -s /home/username/html/yii2app/uploads /home/username/html/yii2app/frontend/web

對於后端

ln -s /home/username/html/yii2app/uploads /home/username/html/yii2app/backend/web

然后像下面這樣在前端或后端使用它們

<img src="/uploads/filename.jpg" />

視窗10

對於使用 Widows10 操作系統在 localhost 上工作的人,想要利用符號鏈接,可以在命令提示符下使用mklink實用程序,它將鏈接名稱作為第一個參數,將源文件夾作為第二個參數。

mklink /D c:\xampp\htdocs\yii2app\frontend\web\uploads c:\xampp\htdocs\yii2app\uploads

注意: /D用於創建指向目錄的符號鏈接

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM