3

I am New in Laravel, and using Laravel 5 I am storing some values in Session like this

This is in a Controller with function say ABC

    Session::put('check_in', $check_in);
    Session::put('check_out', $check_out);
    Session::put('no_of_rooms', $no_of_rooms);
    Session::put('adult', $adult);
    Session::put('child', $child);
    return view('room');

I am getting the values of all these Sessions in rooms view, but now the problem is when I am going to some other link or on other page from this room view and using the Session as

    echo Session::get('check_in')."<br>";
    echo Session::get('check_out')."<br>";
    echo Session::get('no_of_rooms')."<br><br>";
    echo Session::get('adult')."<br>";
    echo Session::get('child')."<br>";

I am not able to get any of these Sessions value.

I am using Sessions so it has to be on all the pages till the Session flashed or browser gets closed, but its not retrieving any of the Session values..

I have seen may topics like this on Stack Overflow, but I am not able to understand those answers, Please Explain me thoroughly and solve this Problem, I am stucked at this part from last 4 to 5 days clueless.......

Manish
  • 43
  • 2
  • 12
  • 1
    What is your session driver? It should be in `config/session.php`. If you are using the `file` driver, make sure your `framework/sessions` folder has write access to your web server. Also make sure you're not `exit()`ing anywhere, the Session data actually gets written to the session at the end of the request, so doing an `exit()` will actually stop it from getting written. – nick.graziano Apr 19 '16 at 05:17
  • My Session Driver is file, And I don't know how to check whether my sessions directory has write access or not, I had deleted to check whether Session files are being made or not but they are not generating, and I am not using exit(). – Manish Apr 19 '16 at 05:41
  • Hello Manish, you should consider reading the documentation before starting a project in a specific framework. You should know first of all the language (php) , the mvc pattern and then start with a framework. If you don't spend time reading the documentation you will have a lot of issues with you web application. Enjoy coding and keep up the good work! – ErvTheDev Apr 19 '16 at 07:20

2 Answers2

0

If your using Laravel 5.*, then you should use 'web' middleware in all routes to use session.

This looks like this: in routes.php in app/http directory

Route::group(['middleware' => 'web'], function () {

   Route::Auth();

   Route::get('/', 'HomeController@index');
   Route::get('/login', 'UserController@userLoginView');
   Route::post('/login', 'UserController@userLogin');
}

You can use session in all those routes by declaring it under 'web' middleware.

I suggest you read more about 'web' middleware in Laravel 5.

Check this out: My Laravel 5.2.10 Sessions wont persist

NOTE: As of Laravel 5.2.27 the web middleware is now in place by default, Try removing the Route::group and see if that helps. https://github.com/laravel/laravel/blob/v5.2.27/app/Providers/RouteServiceProvider.php#L56

Community
  • 1
  • 1
Nelson Melecio
  • 1,334
  • 3
  • 12
  • 19
0

Create a construction __construct() in your controllers that required the user to be connected:

public function __construct()
{
     $this->middleware('auth');
}

and this automatically will check if the user is authenticated if not it will redirect them to login page

Mr.Senhaji
  • 395
  • 5
  • 13