为什么 Laravel 11 出现异常时会话没有启动?

问题描述 投票:0回答:1

我正在使用 Laravel 11。

当我的应用程序出现异常时(例如 404 页面),我注意到会话未启动。就我而言,我想使用此会话(用于翻译 404 页面)。

我是 Laravel 的初学者,但我认为问题就在这里:

return Application::configure(basePath: dirname(__DIR__))
->withRouting(
    web: __DIR__ . '/../routes/web.php',
    commands: __DIR__ . '/../routes/console.php',
    health: '/up',
)
->withMiddleware(function (Middleware $middleware) {
    // Use appendToGroup() to add the middleware to the 'web' group.
    $middleware->appendToGroup('web', Localization::class);
})
->withExceptions(function (Exceptions $exceptions) {
    
})->create();

我尝试在这部分添加启动会话

->withExceptions(function (Exceptions $exceptions) {
    session()->start();
})

没有成功。

解决这个问题的良好做法是什么?

laravel
1个回答
0
投票

我找到了解决方案,而且非常明显。 只需添加一条路线

Route::fallback(function () {
    return view('errors.404');
});

在这种情况下,会话开始!

© www.soinside.com 2019 - 2024. All rights reserved.