Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel session not working between views

Tags:

laravel

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');
});
like image 866
oderfla Avatar asked Feb 19 '26 08:02

oderfla


1 Answers

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
like image 64
Yasin Patel Avatar answered Feb 22 '26 02:02

Yasin Patel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!