Support the ongoing development of Laravel.io →
posted 10 years ago
Session
Last updated 2 years ago.
0

Make sure your remember token is setup correctly on your User model.

See upgrade guide

Last updated 2 years ago.
0

Depending on how I call the controllers method the session either works or doesn't. My app makes a call to a POST route - to add items to a cart which is a session. For some reason the session does not get updated. However if I make a call to the same function with a GET request the function works as expected.

My 'routes.php' contains these two routes:

Route::get('testAdd', array('uses' => 'ProductsController@addToCart'));

Route::post('products/addToCart', array('uses' => 'ProductsController@addToCart'));

Both point to the same method

The method is currently this (for testing):

public function addToCart() {

  Session::put("addcarttest", "add to cart");
  return json_encode(Session::get('addcarttest'));

}

If I call the function with the POST method (with form data) I get the expected result and the contents of the session.

However If I then check for the session (using a profiler) it does not exist. The data did not persist.

If I then call the same method using the GET route, I get the expected result but importantly the session persists.

I thought maybe the POST method deleted sessions however once it exists it stays there - if I try the GET method it creates the session - If I follow that with the POST method again the session stays - so the POST method doesnt delete the session.

This is driving me crazy - I've lost a lot of hours over this and can't see why.

Am I missing something over how Laravel handles POST v GET ? Why would two different methods make a difference to underlying functions?

What do I need to do to make the session work correctly with POST?

Last updated 2 years ago.
0

damienadermann said:

Make sure your remember token is setup correctly on your User model.

See upgrade guide

Yes - confirmed I have followed the guide - I have the database fields - in fact I copied the complete model from the repository to be sure

Last updated 2 years ago.
0

Success!

I found a similar problem relating to laravel 3. For the session to persist in an ajax call I need to return the response correctly.

return json_encode($response);

This is causing the problem. It's not it appears a valid response to enable the session to persist. I changed it to:

return Response::json($response); 

This enabled the session to persist!

For some reason a normal form submit or call to the method allows the first one but ajax does not.

I've seen references elsewhere about echo statements in the method affecting the session data but did not think I had any - the return I suppose must behaving similar to an echo

Happy now (till the next problem)

This is the post that triggered the solution: http://forumsarchive.laravel.io/viewtopic.php?id=1304

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.