3

Its a laravel 5.2 app. I have these 2 functions:

public function page1()
{
        Session::put('test', '1');
        $value = Session::get('test');
        echo 'test: '.$value;
}

public function page2()
{
    $value = Session::get('test');
    echo 'test: '.$value;
}

I first go to localhost/page1. And I can see that the page prints:

test: 1

I then go to localhost/page2

But the page prints:

test:

So it seems like the sessions are not shared among views. Why? Is this some config issue?

This is route.php:

Route::group(['prefix' => 'pages'], function()
{
    Route::get('page1', 'AdminController@page1');
    Route::get('page2', 'AdminController@page2');
});
5
  • Show routes.php file please. Commented May 8, 2016 at 8:54
  • I added route.php to main description. Commented May 8, 2016 at 8:58
  • Which session driver are you using? Commented May 8, 2016 at 9:13
  • I using filesystem. I can see that the session files are saved under storage/frameworks/session Commented May 8, 2016 at 9:20
  • How are you getting test: test in first route? You should get test: 1 Commented May 8, 2016 at 10:02

2 Answers 2

2

i write same code as you in controller as below :

public function page1()
{
      Session::put('test', '1 ');
      $value = Session::get('test');
      echo 'test: '.$value;
}

public function page2()
{
  $value = Session::get('test');
  echo 'test: '.$value;
}

and route file is also same :

Route::group(['prefix' => 'pages'], function()
{
    Route::get('page1', 'AdminController@page1');
    Route::get('page2', 'AdminController@page2');
});

and it is working properly-> Output: http://localhost/laravel-5.2/public/pages/page1

test: 1

http://localhost/laravel-5.2/public/pages/page2

test: 1
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. I also have the same code, but its not working. Strange. Must be somewhere else.
it also strange that. your output is test:test and my output is test:1 . the value of session test
1

There is an issue about session persistence in laravel https://github.com/laravel/framework/issues/8172

If you use Session::save() every time when after update session,may be your problem will solved.So your page1 method will be

public function page1()
{
    Session::put('test', '1');
    Session::save()
    $value = Session::get('test');
    echo 'test: '.$value;
}

1 Comment

didn't help, still no value at page2 :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.