Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Session Flash persists for 2 requests

Recently I have changed to Laravel from Codeigniter, everything was going fine except I encountered a problem with Session::flash.

when I create new user I get success message but It will persist for 2 requests, even I didn't pass the validator: enter image description here

my code in UsersController:

function getCreateUser(){     $config = array(         'pageName' => 'createUser',         'pageTitle' => 'Create User',         'formUrl'   => action('UsersController@postCreateUser'),         'modelFields'   => array(             array('db_name' => 'employee_id', 'text' => 'Employee Id', 'mandatory' => TRUE),             array('db_name' => 'full_name', 'text' => 'Full Name', 'mandatory' => TRUE),             array('db_name' => 'email', 'text' => 'Email', 'mandatory' => FALSE),             array('db_name' => 'password', 'text' => 'Password','value' => '12345', 'mandatory' => TRUE)             ),         'submit_text' => 'Create'         );     return View::make('layouts.form', $config); }  function postCreateUser(){     $config = array(         'pageName' => 'createUser',         'pageTitle' => 'Create User',         'formUrl'   => action('UsersController@postCreateUser'),         'modelFields'   => array(             array('db_name' => 'employee_id', 'text' => 'Employee Id', 'mandatory' => TRUE),             array('db_name' => 'full_name', 'text' => 'Full Name', 'mandatory' => TRUE),             array('db_name' => 'email', 'text' => 'Email', 'mandatory' => FALSE),             array('db_name' => 'password', 'text' => 'Password','value' => '12345', 'mandatory' => TRUE)             ),         'submit_text' => 'Create'         );     $validator = User::validate(Input::all());     if($validator->passes()){         $user = new User(Input::all());         $user->password = Hash::make(Input::get('password'));         $user->Company_id = '1';         $user->save();          Session::flash('message', 'User Created Successfully!');          Session::flash('alert-class', 'alert-success');         return View::make('layouts.form', $config);     }       return View::make('layouts.form', $config)->withErrors($validator->messages()); } 

in form.blade:

@if ( $errors->count() > 0 ) <div class="alert alert-danger">     <p>The following errors have occurred:</p>      <ul>         @foreach( $errors->all() as $message )         <li>{{ $message }}</li>         @endforeach     </ul> </div> @endif 

in master.blade:

@if(Session::has('message')) <p class="alert {{ Session::get('alert-class', 'alert-info') }} alert-dismissable"> {{ Session::get('message') }}</p> @endif 

Maybe I'm not alone with this issue, here is another unanswered question.

Update

For anyone in future facing this problem: Never flash session data without redirecting.

My code now looks like this:

function postCreateUser(){     $validator = User::validate(Input::all());     if($validator->passes()){         $user = new User(Input::all());         $user->password = Hash::make(Input::get('password'));         $user->Company_id = '1';         $user->save();          Session::flash('message', 'User Created Successfully!');          Session::flash('alert-class', 'alert-success');     } else {         Session::flash('message', Helpers::formatErrors($validator->messages()->all()));         Session::flash('alert-class', 'alert-danger');     }      return Redirect::action('UsersController@getCreateUser'); } 
like image 318
Walid Ammar Avatar asked Jul 04 '14 18:07

Walid Ammar


People also ask

How long does session last Laravel?

You need to understand what happened: You've set the lifetime of the sessions to 120 minutes, which means after 120 minutes the session is flushed. The remember_me feature is using cookies. If there is no user session Laravel checks the cookies and recreates the session if the session cookie is still valid.

What is the best use of flash sessions in Laravel?

Flash messages is required in laravel application because that way we can give alter with what progress complete, error, warning etc. In this tutorial i added several way to give flash message like redirect with success message, redirect with error message, redirect with warning message and redirect with info message.

How does Laravel store data in session?

Data can be stored in session using the put() method. The put() method will take two arguments, the “key” and the “value”.

What is session regenerate Laravel?

Laravel utilize basically the original session file store but acts a little bit different. Beside changing the directory they are saved, when you call the regenerate function it creates another session file and deletes the old one. You can see it the implementation Illuminate\Session\Store. php .


2 Answers

You are Flashing session data and creating a view instead of redirecting, meaning the message will Flash for this request and for the next one, showing twice.

If you want to show the message on the current request without redirecting, I would suggest providing the errors to your View::make instead of trying to Flash the messages. If you MUST Flash the message on the current request, then you will need to Session::forget('key') or Session::flush() after your view.

like image 102
Drew Avatar answered Sep 22 '22 02:09

Drew


The following seems to be available from version 5.1 onward. Although the method is not mentioned on Laravel's documentation page about sessions.

session()->now() 

This is the same as flash, except it won't persist to the next request.

like image 42
aross Avatar answered Sep 22 '22 02:09

aross