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

oymdgrw7  于 2022-12-21  发布在  PHP
关注(0)|答案(2)|浏览(107)

我想在我的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');
});
ovfsdjhp

ovfsdjhp1#

RouteServiceProvider在StartSession中间件之前启动,无法访问路由文件中的会话,请使用中间件检查

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/中间件/会话哈希索引.php

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

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

应用程序/Http/内核.php

protected $middlewareGroups = [
    'web' => [
        ...
        'session.has.index' => , \App\Http\Middleware\SessionHasIndex::class       
        ...
    ],
erhoui1w

erhoui1w2#

启动路由前加载会话

protected $middlewareGroups = [
    'web' => [
        \Illuminate\Session\Middleware\StartSession::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');
    });

相关问题