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

onurzdgn's avatar

Session

Hi everyone. I am using Laravel 10 I have an problem with sessions. I will try to change language with session how ever I can't do this and I can't find the problem to. I can't create any controller for this, I am using function on web.php.

This is my web.php:

Route::get('lang/{lang}', function ($lang) {
    session()->put('lang', $lang);
    return redirect()->back();
})->name('lang');

This is my button:

<a class="site-logo" href="{{ route('lang', 'en') }}">
	<i class="fa-thin fa-language "></i>
</a>

And I create an middleware for change app language:

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

class SetLocale
{
    /**
     * Handle an incoming request.
     *
     * @param  \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response)  $next
     */
    public function handle(Request $request, Closure $next): Response
    {
        if (session()->has('lang')) {
            app()->setLocale(session('lang'));
        }
        return $next($request);
    }
}
0 likes
3 replies
onurzdgn's avatar

@martinbean Language not change. I try to this:

        if (session()->has('lang')) {
            app()->setLocale(session('lang'));
        }
		echo app()->getLocale();
        return $next($request);

Local name not changed

onurzdgn's avatar
onurzdgn
OP
Best Answer
Level 2

Okay I found problem. I can't added middleware to Kernel.php

Please or to participate in this conversation.