我想在我的web.php文件中使用session变量。因为我想从session中根据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');
});
你能帮我解决这个问题吗?
来自kernel.php文件的中间件组:
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
// \Illuminate\Session\Middleware\AuthenticateSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
\App\Http\Middleware\Localization::class,
],
'api' => [
// \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
'throttle:api',
\Illuminate\Routing\Middleware\SubstituteBindings::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');
});
2条答案
按热度按时间ovfsdjhp1#
RouteServiceProvider在StartSession中间件之前启动,无法访问路由文件中的会话,请使用中间件检查
创建中间件
更新中间件检查会话,如果没有对应的会话,则中止请求:
应用程序/Http/中间件/会话哈希索引.php
安装中间件,以便路由可以使用中间件
应用程序/Http/内核.php
erhoui1w2#
启动路由前加载会话