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

mikromike's avatar

How to pass App:getLocale to session key applocale ?

hello

I am building online language swicher, without add extra prefix per landuage like /en or /fi.

in app.blade

 <html lang="{{App::getLocale() }}">
 <head>
    @include('parts.head')
      @if(!session()->has('applocale'))
        {{  session()->put('applocale',App::getLocale() )  }}

        @dd((App::getLocale()))

          {{-- session()->put('applocale', 'huhuu') --}}
    @endif

I tried to pass {{App::getLocale() }} to session as value for applocale. but this way it's not work.

if I pass string, it works. so looks like session value need to be string.

even dd did not give me anything.

so How to pass App:getLocale to session key applocale ?

Thank Mika.

0 likes
7 replies
RayC's avatar

When I add this to a blade file it comes out as expected

{{  session()->put('applocale', App::getLocale() )  }}
{{ session()->get('applocale') }}

Prints: en

RayC's avatar

Also, if you want to use dd() in a blade file, you need to use it like this:

{{ dd(App::getLocale()) }}
mikromike's avatar

@RayC shows 'en'. but debugbar session shows applocale => 'fi'.

So I was thinking if request_headers "accept-language" => array:1 [▼ 0 => "en-GB,en;q=0.5" overwrite my session value ? and config/app.php has 'locale' => 'fi',

too many places for setup simple value.

mikromike's avatar

{{ session()->get('applocale') }} - {{ App::getLocale() }}

first one is empty, second shows "en". debugbar shows applocale "fi". if website sees session key empty, but debugbar shows value, could be possiable that fortify logoutreponse still keep applocale key somewhre in memory ? and pass it to guest mode back to session ?

RayC's avatar

@mikromike Calling {{ App::getLocale() }} will show the default language you have set in your config/app.php file, in your case appears to be 'fi'. That is why you are still seeing it.

mikromike's avatar
  Here is my destroyt method from logincontroller.


    public function destroy(Request $request): LogoutResponse
   {
      Auth::guard()->logout();
        $request->session()->forget('applocale');
        $request->session()->flush();
        $request->session()->invalidate();
        $data = $request->session()->all();
   //      dd($data);

           return app(LogoutResponse::class);
     }

Please or to participate in this conversation.