image - 显示存储在 laravel storage/app/protected/images 中的图像但返回 404
问题描述
该应用程序将图像存储在 Laravel 7 上 storage/app/protected/images 目录内的各种目录中。我在本地主机上显示从这些目录检索到的图像没有问题,但是当我移动到暂存时,我注意到每个请求都会返回404 即使图像显示在浏览器中。
我已经检查了后端,并且后端(管理员)中的所有图像都有相同的问题。
图片 URL 结构很简单:example.com/images/slider/image-name.png
这是我的ImageViewController
:
namespace App\Http\Controllers\Client;
use App\Exceptions\ErrorPageException;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Storage;
class ImageViewController extends Controller
{
public function getMainImage($name)
{
if(file_exists(storage_path('app/protected').'/images/'.$name)){
$image = storage_path('app/protected').'/images/'.$name;
}else{
throw new ErrorPageException(404);
}
return response()->file($image);
}
public function getImage($type, $name)
{
if(file_exists(storage_path('app/protected').'/images/'.$type.'/'.$name)){
$image = storage_path('app/protected').'/images/'.$type.'/'.$name;
}else{
throw new ErrorPageException(404);
}
return response()->file($image);
}
}
这是我的路线:
Route::get('images/{name}', 'Client\ImageViewController@getMainImage');
Route::get('images/{type}/{name}', 'Client\ImageViewController@getImage');
我似乎无法自己解决问题。
请帮忙!
解决方案
解决了!
包含 NGINX 静态文件缓存指令后出现问题。
我改变了方法,现在一切正常!
它们适用于我这种情况的任何人:
# browser caching of static assets
location ~ /\.(jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc)$ {
expires 30d;
add_header Pragma public;
add_header Cache-Control "public";
}
# CSS and Javascript
location ~* \.(?:css|js)$ {
expires 1y;
access_log off;
add_header Cache-Control "public";
}
所有这些代码都在server { ... }
推荐阅读
- php - 在带有控制器的模型中添加数据透视表的 Laravel 路由不工作
- python - 如何在 Flask 中找到用 for 循环写入的数据
- pandas - 如何在 numpy/pandas 中处理来自 spark 的大型镶木地板文件?
- javascript - 在 Javascript 中使用 .map 函数创建元素
- ruby-on-rails - 在rails中查询belongs_to关系时如何使用通配符?
- reactjs - React useMutation 没有传递正确的参数输入
- reactjs - 当钩子在多个地方使用时,自定义钩子中的 useEffect 会冻结我的 react-native 应用程序
- json - 尝试在 laravel 中添加对象时,json 文件被完全覆盖
- php - 无法使用 FPDI 编辑现有 PDF 文件
- java - vscode-java 自带代码分析吗?