Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

kickthemooon's avatar

Set session variable at runtime

I need to set a session variable anytime a visitor visits my site. I am not sure where to set that session variable?

Where can I set a session variable at app runtime?

0 likes
6 replies
kickthemooon's avatar

@Moted yea i read that, maybe its me but i cant seem to find one place for setting a session variable for any new session the user makes to my app the first time he opens it... I am not sure if you understand me.

I need one place to declare this variable at "runtime", at "boot" at session start because the session starts immediately the user opens my app / website so I need to set that variable at session start.... it needs to be available immediately a user opens my website :)

Moted's avatar

Well if you are using Laravel 5.2, you notice that at Kernel.php

   protected $middlewareGroups = [
        'web' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
        ],

So basically if you put anything into route:

Route::group(['middleware' => ['web']], function () { 
    Route::get('/', function () {
        return 'Try to check if you can access your session';
    });
});
1 like
Moted's avatar

Btw if still having problems checking like that

Route

Route::group(['middleware' => ['web']], function () { 
    Route::get('/',  'TestController@home'    });
});
--------------------------- at TestController ----------------------------------------
  public function home(Request $request)
    {
        dd($request->session());
    }
kickthemooon's avatar
kickthemooon
OP
Best Answer
Level 5

I solved it at the middleware, wasnt aware that my colleague set up a language middleware so the middleware looks like this now:

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Foundation\Application;
use Illuminate\Http\Request;
use Illuminate\Routing\Redirector;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Session;

class Language
{
    public function handle($request, Closure $next)
    {
        if (Session::has('applocale') AND array_key_exists(Session::get('applocale'), Config::get('languages'))) {
            App::setLocale(Session::get('applocale'));
        } else { // This is optional as Laravel will automatically set the fallback language if there is none specified
            App::setLocale(Config::get('app.fallback_locale'));
            Session::set('applocale', Config::get('app.fallback_locale'));
        }
        return $next($request);
    }
}

I am setting the applocale variable from the app.fallback_locale
and I am including the middleware in the kernel web middleware group:

protected $middlewareGroups = [
        'web' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
            \App\Http\Middleware\Language::class,
        ],

        'api' => [
            'throttle:60,1',
        ],
    ];

Please or to participate in this conversation.