You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Laravel中不使用Symlink访问storage/app/public/路径?

在Laravel中不使用符号链接访问storage/app/public路径的方法

当然可以,下面是几种可行的方案:

1. 通过Web服务器配置直接映射

直接让Web服务器(Nginx/Apache)将请求指向storage/app/public目录,无需修改Laravel代码:

Nginx配置

在站点的server配置块中添加以下规则:

location /storage/app/public {
    alias /你的Laravel项目绝对路径/storage/app/public;
    expires 30d; # 配置缓存,提升性能
}

Apache配置

在项目根目录的public/.htaccess文件中添加以下规则:

RewriteRule ^storage/app/public/(.*)$ /你的Laravel项目绝对路径/storage/app/public/$1 [L]

配置完成后,就能直接通过domain.com/storage/app/public/image/item.png访问对应文件。

2. 自定义路由返回文件响应

在routes/web.php中添加路由,处理对应路径的请求并返回文件:

Route::get('storage/app/public/{path}', function ($path) {
    $filePath = storage_path('app/public/' . $path);
    
    if (!file_exists($filePath)) {
        abort(404);
    }
    
    return response()->file($filePath);
})->where('path', '.*');

这个路由会匹配所有storage/app/public/开头的请求,验证文件存在后返回文件内容,完全不需要符号链接。

3. 调整文件系统磁盘配置(替代方案)

如果可以接受将公共资源直接存储到public/storage目录,修改config/filesystems.php中的public磁盘配置:

'public' => [
    'driver' => 'local',
    'root' => public_path('storage'),
    'url' => env('APP_URL').'/storage',
    'visibility' => 'public',
],

之后上传到public磁盘的文件会直接存入public/storage,可以通过domain.com/storage/image/item.png访问,同样无需符号链接。

内容的提问来源于stack exchange,提问作者Khalid

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 21:22:40