Laravel session does not work on another page

Asked

Viewed 867 times

2

I am working on a project with Laravel, in it I created a session variable that takes a parameter of the url as follows:

if(!empty(Input::get('lang'))){
    $lang = Input::get('lang');
    Session::put('lang', $lang);

} else {
    $lang = "es";
    Session::put('lang', $lang);
}

if(Session::get('lang') == 'pt-br'){
    App::setLocale("pt-br");
}

That way the session is only working on this page, when I try to use on another page I can’t, someone could help me?

  • Look in the config folder for the file Session.php and see its Lifetime.

  • It’s at the default value, 120. Even if I change it to a very high value it’s still the same.

  • It comes empty on the other page ?

  • The return is NULL.

  • There is some logic error there! check the code better, or else put all the code!

  • @Lucianoalexandre a tip would be you create a middleware to do this language treatment, in middleware you can take the lang parameter in the URL and set the correct language and if you do not have the parameter you arrow the default language.

Show 1 more comment

1 answer

1

To Session() of Laravel works in several ways, as we can see in documentation. However, if you are using the standard form you will need to give a Reflash() or a Keep() in your session to keep on a next page:

$request->session()->reflash();

or

$request->session()->keep('lang');

Using the $request as the request variable.

I hope I’ve helped!

  • It helped yes, but could clarify one more detail, what is a request variable? How do I use?

  • Of course! This variable already comes automatic in a request as it gives a get there on your Routes.php. I will post an example...

  • Here’s an example... in your route file there’s a get for a particular controller, right? with a function, for example list. In the function inside the controller the function list should be the way it is here in this file, with the variable request: https://gist.github.com/evertramos/860bba5f8a4c672c829d662af3fc3b13 see this template.

  • It is possible to create a session variable without using a request variable?

  • The request variable is Laravel’s default... I don’t know if you can create the session variable outside the request, because the session is saved in the client’s back and forth request.

  • When trying to use it I am getting the following error: Argument 1 passed to Homecontroller::updateLanguage() must be an instance of Illuminate Http Request, None Given

  • One detail that may be important is that I am using version 4.2 of the Standard.

  • You have to reference in the Controller. I use 5.2

  • My code looks like this in the controller: public Function updateIdioma(Request $resquest){ if(!Empty(Input::get('lang'))){ $lang = Input::get('lang'); $langAtual = $request->Session()->get($lang); Return View::make('auth.Registration'); } }

  • Try to make the models of the documentation and to give birth from there leave for the examples before putting into production.

  • I believe that this structure is not compatible with version 4.2.

  • I don’t understand why I use reflash, AP does not use flash nowhere in the code. When using put, the session should work with get normally.

Show 7 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.