无法访问web.php中的会话数据



我想在我的web.php文件中使用会话变量。因为我想根据会话current_index创建动态路由。并且current_index会根据不同的登录而改变。

$index = Session::get('current_index');

Route::prefix($index)->group(function () {
Route::get('index', [SystemConfigController::class, 'index'])->name('systemConfig.index');
Route::post('list', [SystemConfigController::class, 'index'])->name('systemConfig.list');
Route::post('testmail', [SystemConfigController::class, 'testMail'])->name('systemConfig.testmail');
Route::post('edittable', [SystemConfigController::class, 'editTable'])->name('systemConfig.edittable');
});

你能帮我解决这个问题吗?middlewareGroups from kernel.php:

protected $middlewareGroups = [
'web' => [
AppHttpMiddlewareEncryptCookies::class,
IlluminateCookieMiddlewareAddQueuedCookiesToResponse::class,
IlluminateSessionMiddlewareStartSession::class,
// IlluminateSessionMiddlewareAuthenticateSession::class,
IlluminateViewMiddlewareShareErrorsFromSession::class,
AppHttpMiddlewareVerifyCsrfToken::class,
IlluminateRoutingMiddlewareSubstituteBindings::class,
AppHttpMiddlewareLocalization::class,
],
'api' => [
// LaravelSanctumHttpMiddlewareEnsureFrontendRequestsAreStateful::class,
'throttle:api',
IlluminateRoutingMiddlewareSubstituteBindings::class,
],
];

编辑如下:

我有这样的场景。我必须像下面这样创建动态路由:

$index = Session::get('current_index');

如果$index为0,那么我的路由列表如下:

Route::prefix(0)->group(function () {
Route::get('index', [SystemConfigController::class, 'index'])->name('systemConfig.index');
Route::post('list', [SystemConfigController::class, 'index'])->name('systemConfig.list');
Route::post('testmail', [SystemConfigController::class, 'testMail'])->name('systemConfig.testmail');
Route::post('edittable', [SystemConfigController::class, 'editTable'])->name('systemConfig.edittable');
});

如果$index为1,那么我的路由列表如下:

Route::prefix(1)->group(function () {
Route::get('index', [SystemConfigController::class, 'index'])->name('systemConfig.index');
Route::post('list', [SystemConfigController::class, 'index'])->name('systemConfig.list');
Route::post('testmail', [SystemConfigController::class, 'testMail'])->name('systemConfig.testmail');
Route::post('edittable', [SystemConfigController::class, 'editTable'])->name('systemConfig.edittable');
});

RouteServiceProvider在StartSession中间件之前被启动,所以你不能在路由文件中访问session。使用中间件来检查

Route::middleware('session.has.index')->prefix($index)->group(function () {
Route::get('index', [SystemConfigController::class, 'index'])->name('systemConfig.index');
Route::post('list', [SystemConfigController::class, 'index'])->name('systemConfig.list');
Route::post('testmail', [SystemConfigController::class, 'testMail'])->name('systemConfig.testmail');
Route::post('edittable', [SystemConfigController::class, 'editTable'])->name('systemConfig.edittable');
});

创建中间件

php artisan make:middleware SessionHasIndex

更新中间件检查会话,如果没有相应的会话,中止请求:

应用程序/Http/中间件/SessionHasIndex.php

public function handle($request, Closure $next) 
{ 
$request->route()->prefix(Session::get('index')); 
return $next($request); 
} 

安装中间件,以便路由可以使用中间件

app/Http/Kernel.php

protected $middlewareGroups = [
'web' => [
...
'session.has.index' => , AppHttpMiddlewareSessionHasIndex::class       
...
],

在初始化路由之前加载会话

protected $middlewareGroups = [
'web' => [
IlluminateSessionMiddlewareStartSession::class, 
],
];

Route::prefix($index)
->middleware('web')
->group(function () {
Route::get('index', [SystemConfigController::class, 'index'])->name('systemConfig.index');
Route::post('list', [SystemConfigController::class, 'index'])->name('systemConfig.list');
Route::post('testmail', [SystemConfigController::class, 'testMail'])->name('systemConfig.testmail');
Route::post('edittable', [SystemConfigController::class, 'editTable'])->name('systemConfig.edittable');
});