[英]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 目錄中的符號鏈接並查看圖像。
在frontend
和backend
的web
目錄中創建一個符號鏈接。
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" />
對於使用 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.