Laravel 10升级至11后运行Artisan命令触发致命错误求助
Laravel 11升级后Artisan命令路径错误修复
问题描述
升级Laravel从10到11后,执行任意Artisan命令均触发致命错误:
Fatal error: Uncaught Error: Failed opening required 'C:\LaravelVOPlanning\admin/bootstrap/app.php' (include_path='.;c:\php\includes') in C:\Laravel\VOPlanning\admin\artisan:12
报错代码行:
// Bootstrap Laravel and handle the command... $status = (require_once __DIR__.'/bootstrap/app.php') ->handleCommand(new ArgvInput);
修复方案
1. 替换Artisan文件为Laravel 11版本
Laravel 11重构了应用启动流程,移除了bootstrap/app.php,原Laravel 10的Artisan代码不再适用。将当前Artisan文件内容替换为以下官方模板代码:
#!/usr/bin/env php <?php use Illuminate\Foundation\Application; use Illuminate\Foundation\Configuration\Exceptions; use Illuminate\Foundation\Configuration\Middleware; require __DIR__.'/vendor/autoload.php'; $app = Application::configure(basePath: dirname(__DIR__)) ->withRouting( web: __DIR__.'/routes/web.php', commands: __DIR__.'/routes/console.php', health: '/up', ) ->withMiddleware(function (Middleware $middleware) { // 这里可添加你的中间件配置 }) ->withExceptions(function (Exceptions $exceptions) { // 这里可添加你的异常处理配置 })->create(); $app->run();
2. 适配Laravel 11项目结构
- 删除旧的
bootstrap/app.php文件(Laravel 11已不再使用) - 确保
bootstrap/providers.php存在,该文件用于注册服务提供者,示例内容:
<?php return [ App\Providers\AppServiceProvider::class, // 添加其他需要注册的服务提供者 ];
3. 重新生成自动加载
执行以下命令确保依赖加载正常:
composer dump-autoload
4. 路径问题说明
错误提示中的C:\LaravelVOPlanning\admin是路径拼接错误(实际路径为C:\Laravel\VOPlanning\admin),替换Artisan文件后,新启动逻辑通过dirname(__DIR__)正确识别项目根目录,可解决该拼接异常。
内容的提问来源于stack exchange,提问作者Jim
相关产品推荐
相关产品推荐

