Laravel 9自定义FormRequest抛出“Illuminate\Session\Store对象不可调用”错误求助
Hey there, let's work through this frustrating error you're hitting with Laravel 9's custom FormRequest classes. That "Session Store is not callable" message points to a problem with how the session is initialized or accessed when your FormRequest is resolved. Here are the actionable steps to fix it:
1. Verify Middleware Order in Kernel.php
This is the most common culprit. FormRequest relies on the session being fully initialized before it's resolved, so the StartSession middleware must run before ShareErrorsFromSession in your web middleware group.
Open app/Http/Kernel.php and check the $middlewareGroups['web'] array. Make sure it follows this order:
'web' => [ \App\Http\Middleware\EncryptCookies::class, \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class, \Illuminate\Session\Middleware\StartSession::class, // Must come first \Illuminate\View\Middleware\ShareErrorsFromSession::class, // Must be after StartSession \Illuminate\Foundation\Http\Middleware\VerifyCsrfToken::class, \Illuminate\Routing\Middleware\SubstituteBindings::class, ],
If these were out of order, Laravel tries to access the session before it's been started, leading to that weird "not callable" error.
2. Clear All Caches and Regenerate Autoload Files
Cached container bindings or configs can sometimes cause wonky dependency injection issues. Run these commands in your terminal:
php artisan cache:clear php artisan config:clear composer dump-autoload
After running these, restart your local server (if you're using php artisan serve, stop it and start it again) to ensure the changes take effect.
3. Check Third-Party Package Compatibility
You mentioned using spatie/laravel-permission—make sure you're running a version compatible with Laravel 9. Older versions might have middleware or container binding conflicts that interfere with FormRequest's session access. Verify the package's README or composer.json for compatibility details.
4. Double-Check Route Middleware
Even though you said you added the 'web' middleware, confirm your update route is definitely within the web middleware group. For example, in your routes/web.php file, the route should be defined there (since web.php is automatically wrapped in the web middleware group by default) or explicitly assigned:
Route::put('/users/{user}', [UserController::class, 'update'])->middleware('web');
Why This Happens
Looking at your error stack trace, the issue originates in Symfony\Component\HttpFoundation\Request->getSession() where it tries to invoke the session handler. If the session hasn't been initialized (because middleware ran out of order) or the container has a stale/bad binding for the session, it tries to call the Store object directly instead of the proper session resolver—hence the "not callable" error. FormRequest needs the session to handle validation flash messages, so this initialization step is critical.
Give these steps a try—most of the time, fixing the middleware order and clearing caches resolves this issue.
内容的提问来源于stack exchange,提问作者Alex

