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

Laravel 11如何禁用storage路由?解决私有文件可直接访问问题

解决Laravel 11中禁用默认storage/{path}路由的问题

要移除Laravel 11默认注册的storage/{path}路由,只需在项目的bootstrap/app.php文件中添加withoutStorageRouting()方法,具体操作如下:

  1. 打开bootstrap/app.php文件,找到应用配置的链式调用代码块
  2. 在应用配置链中插入->withoutStorageRouting()(位置可在->withRouting(...)之前或其他合适位置)
  3. 保存文件后,执行php artisan route:list验证路由是否已移除

修改后的bootstrap/app.php示例:

return Application::configure(basePath: dirname(__DIR__))
    ->withoutStorageRouting()
    ->withRouting(
        web: __DIR__.'/../routes/web.php',
        api: __DIR__.'/../routes/api.php',
        commands: __DIR__.'/../routes/console.php',
        health: '/up',
    )
    ->withMiddleware(function (Middleware $middleware) {
        // 你的中间件配置
    })
    ->withExceptions(function (Exceptions $exceptions) {
        // 你的异常处理配置
    })
    ->create();

额外说明

  • 禁用该路由后,storage/app/public目录的文件仍可通过public/storage软链接直接访问(这是Web服务器直接处理的,无需Laravel路由)
  • 私有文件应放在storage/app/private目录下,该目录不会被Web直接访问;若需要授权访问这些文件,需通过自定义控制器方法实现(例如验证用户权限后返回response()->file())

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.17 17:04:59