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

将public改为public_html后,如何正确配置Laravel存储软链接?

1. 清除配置缓存

Laravel会缓存配置文件,旧的public路径可能仍在缓存中,先执行以下命令清除缓存:

php artisan config:clear
php artisan cache:clear

2. 验证public_path()是否正确

通过Artisan Tinker确认路径配置是否生效:

php artisan tinker

在Tinker环境中执行:

echo public_path();

如果输出不是项目根目录下的public_html,请排查以下配置:

  • 检查AppServiceProvider.php的register方法,确认路径绑定无拼写错误:
    public function register(): void
    {
        $this->app->bind('path.public', function () {
            return base_path() . '/public_html';
        });
    }
    
  • 修改bootstrap/app.php中的usePublicPath配置,去掉realpath避免路径解析失败:
    ->registered(function ($app) {
        $app->usePublicPath(base_path('public_html'));
    })
    

3. 手动创建软链接

若上述配置生效后,php artisan storage:link仍无法正确创建链接,可手动创建软链接:

Linux/macOS

ln -s 项目根目录路径/storage/app/public_html 项目根目录路径/public_html/storage

Windows(命令提示符)

mklink /D "项目根目录路径\public_html\storage" "项目根目录路径\storage\app\public_html"

Windows(PowerShell)

New-Item -ItemType SymbolicLink -Path "项目根目录路径\public_html\storage" -Target "项目根目录路径\storage\app\public_html"

4. 额外检查项

  • 确认config/filesystems.php中links配置项的public_path('storage')已指向public_html/storage
  • 确保服务器用户对public_html和storage目录拥有读写权限

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.22 04:06:02