laravel destroy session after change language
I have a problem on a Laravel 10.x project I'll start by saying that I have always used the same procedure and have never had any problems of this type
I have a multilingual site, customers arrive on the home page, make a transfer reservation by entering departure, Arrival and departure time when they press the "booking" button they are redirected to a summary page where they are saved via controller all data in session
this is my route
Route::any('/riepilogo',[HomeController::class,'riepilogo'])->name('riepilogo')->middleware('lang');
and this is my controller
public function riepilogo(Request $request){
//dd($request->all());
$partenza=$request->indirizzo_partenza;
$arrivo=$request->indirizzo_arrivo;
$pax=$request->pax;
$trolley=$request->trolley;
$valigie=$request->valigie;
$bici=$request->bici;
$data_partenza=$request->data_partenza;
$data_arrivo=$request->data_arrivo;
$ora_arrivo=$request->ora_arrivo;
$ora_partenza=$request->ora_partenza;
Session()->put('indirizzo_partenza',$partenza);
Session()->put('indirizzo_arrivo',$arrivo);
Session()->put('pax',$pax);
Session()->put('trolley',$trolley);
Session()->put('valigie',$valigie);
Session()->put('bici',$bici);
Session()->put('data_partenza',$data_partenza);
Session()->put('data_arrivo',$data_arrivo);
Session()->put('ora_arrivo',$ora_arrivo);
Session()->put('ora_partenza',$ora_partenza);
$stati=State::all();
//dd(Session()->all());
Session()->save();
return view('riepilogo',compact('stati'));
}
the data is saved correctly and the session data relating to the booking is displayed in the summary form.
the problem occurs as soon as I try to change the language from this page
<form action="{{ route('locale', 'it') }}" method="POST">
@csrf
<button class="nav-link" style="background:transparent;border:none;">
<img src="/image/italal.png" class="flag-icon flag-icon w-50">
</button>
</form>
<form action="{{ route('locale', 'en') }}" method="POST">
@csrf
<button class="nav-link" style="background:transparent;border:none;">
<img src="/image/engl.png" class="flag-icon flag-icon w-50">
</button>
</form>
this is route for change lang
Route::post('/traduzione/{locale}',[HomeController::class,'traduzione'])->name('locale');
function traduzione():
public function traduzione(Request $request,$locale)
{
$allreq=$request->all();
session()->put('locale', $locale);
App::setLocale($locale);
return redirect()->back()->withInput($allreq);
}
and for information this is the middleware
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Session;
class Localization
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle(Request $request, Closure $next)
{
if (Session::has('locale')) {
App::setLocale(Session::get('locale'));
}
dd(Session()->all());
return $next($request);
}
}
the problem occurs right here, because when you return to the summary page, the form is no longer filled in with the session data and the session itself dd(Session()->all()) is empty
the strange thing I noticed is that if I go back from the browser and reload the page, I have the session data and the language selected again, I don't know how to solve it, can anyone help me?
Please or to participate in this conversation.