skip to Main Content

I want to use session variable in my web.php file. As I want to create dynamic route according to current_index from session. And current_index is changed as per different login.

$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');
        });

Can you please help me to figure out this issue?

middlewareGroups from kernel.php file :

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,
        ],
    ];

Edited below :

I have scenario like this. I have to create dynamic route like below:

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

If $index is 0 then my route list is like below :

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');
});

If $index is 1 then my route list is like below :

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

Answers


  1. Load session before initiating routes

    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');
        });
    
    Login or Signup to reply.
  2. RouteServiceProvider are booted before the StartSession middleware, so you cannot access session in route files. Use middleware to check instead

    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');
    });
    

    create middleware

    php artisan make:middleware SessionHasIndex
    

    Update middlewares to check the session, if it does not have corresponding session, abort the request:

    app/Http/Middleware/SessionHasIndex.php

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

    Install Middlewares, so routing can use the middlewares

    app/Http/Kernel.php

    protected $middlewareGroups = [
        'web' => [
            ...
            'session.has.index' => , AppHttpMiddlewareSessionHasIndex::class       
            ...
        ],
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search