Laravel从5.8升级到7.29后Web路由报InputBag类型错误
Laravel 5.8升级到7.29后Web路由报错问题
报错详情
传递给Symfony\Component\HttpFoundation\InputBag::get()的参数1必须是string类型,传入了null,调用位置在D:\Syed Ghazanfar\Futafut\Backend\v2\vendor\laravel\framework\src\Illuminate\Session\Middleware\StartSession.php第157行
将Laravel从5.8版本升级到7.29后,仅web.php路由出现上述错误,api路由运行正常。已按照Laravel官方升级指南操作,尝试过注释所有web路由、执行php artisan optimize:clear,甚至创建仅返回1的简单路由,仍出现相同错误。
报错截图:
RouteServiceProvider.php代码
<?php namespace App\Providers; use Illuminate\Support\Facades\Route; use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider; class RouteServiceProvider extends ServiceProvider { /** * This namespace is applied to your controller routes. * * In addition, it is set as the URL generator's root namespace. * * @var string */ protected $namespace = 'App\Http\Controllers'; /** * Define your route model bindings, pattern filters, etc. * * @return void */ public function boot() { // parent::boot(); } /** * Define the routes for the application. * * @return void */ public function map() { $this->mapApiRoutes(); $this->mapWebRoutes(); // } /** * Define the "web" routes for the application. * * These routes all receive session state, CSRF protection, etc. * * @return void */ protected function mapWebRoutes() { Route::middleware('web') ->namespace($this->namespace) ->group(base_path('routes/web.php')); } /** * Define the "api" routes for the application. * * These routes are typically stateless. * * @return void */ protected function mapApiRoutes() { Route::prefix('api') ->middleware('api') ->namespace($this->namespace) ->group(base_path('routes/api.php')); } }
解决方案
- 检查
config/session.php配置:确认cookie参数为有效字符串,Laravel 7.x要求该参数不能为null,而5.8版本可能允许默认值为null。 - 清除浏览器缓存与Cookie:升级后旧的Session Cookie可能引发冲突,清除后重新访问测试。
- 排查自定义Session中间件:如果有自定义的Session相关中间件,检查是否在调用
InputBag::get()时传入了null参数。 - 重新发布Session配置文件:执行
php artisan vendor:publish --tag=laravel-session,覆盖旧配置,确保使用7.x版本的默认配置。 - 检查.env文件:确认
SESSION_COOKIE变量已设置且值为字符串,例如SESSION_COOKIE=laravel_session。
内容的提问来源于stack exchange,提问作者Ghazanfar Syed
相关产品推荐
相关产品推荐

