Laravel admin 使用者頭像顯示不出的原因及解決方法

zhaiduting發表於2018-12-08

已經使用命令 php artisan storage:link 建立過軟連結了,頭像仍然不顯示。
發現連結顯示的是 http://localhost/storage/images/rar.jpg 導致了404錯誤。
如果頭像連結是 http://yw.pay/storage/images/rar.jpg 那就對了!
file
方法一:
將.env 檔案裡的 APP_URL=http://localhost
修改為 APP_URL=http://yw.pay 重新整理瀏覽器,發現頭像成功顯示。
但這是個治標不治本的壞辦法,放棄。

方法二:
將 config/admin.php 裡的 'disk' => 'public'
修改為 'disk' => 'admin'
在 config/filesystems.php 裡面新增一個 admin 磁碟,重新整理瀏覽器頭像也能顯示!
'disks' => [
'admin'=>[
'driver'=>'local',
'root'=>storage_path('app/public'),
]
……
],
file
此法的妙處在於頭像連結使用的是相對路徑 /storage/images/rar.jpg
之前什麼 http://localhost/storage/images/rar.jpg
或者什麼 http://yw.pay/storage/images/rar.jpg
都是絕對路徑,連結寫死了。
對於虛擬主機來說,寫死的連結未必會顯示(要看虛擬主機的主機名)
但是相對路徑 /storage/images/rar.jpg 一定會顯示!只要檔案存在。

相關文章