5

I have a wired problem. Im put in session some string/object, and when i want to get it is not there.

This is the code:

class CartController extends \BaseController
{

    public function index()
    {
        return Session::all(); // items is not there
    }

    public function store()
    {
        Session::put('items', Input::get('items'));
    }

}

in angular:

this.saveItemsToSession = function() {
    $http.post('cart', {
      items: 'test even string'
    });
  };

What can cause this problem?

Its seems like Session dont work. This way is working:

session_start();
class CartController extends \BaseController
{

    public function index()
    {
        return $_SESSION['items'];
    }

    public function store()
    {
        $_SESSION['items'] = Input::get('items');
    }

}

3 Answers3

3

From laravel docs : https://laravel.com/docs/master/routing

Any routes not placed within the web middleware group will not have access to sessions and CSRF protection, so make sure any routes that need these features are placed within the group. Typically, you will place most of your routes within this group:

Route::group(['middleware' => ['web']], function () {
     //all routes
});

Put your all routes inside middleware and your problem will solve.

Mr. Engineer
  • 3,522
  • 4
  • 17
  • 34
-1
    Session::all()

return array of all session variables, while

    $_SESSION['items']

return session of 'items', and both output are different. To get the

    $_SESSION['items']

value, Here are a few ways:

    Session::all()['items']

or

    Session::get('items')
Eng Cy
  • 1,527
  • 11
  • 15
-3

Are you using sub-domains at all?

Ewan Valentine
  • 3,741
  • 7
  • 43
  • 68