1
class FileController extends Controller
{
    public function login()
    {
        /*
         * TODO: Handle via CAS
         * Hardcoded for demo purposes
         */
        Session::put('isLogged', true);
        Session::put('index', "123456");

        return View::make('login');
    }

    public function user()
    {
        if(Session::get('isLogged') == true )
            return View::make('user');
    }
}

I have the following code. There is a link on login that goes to the FileControllers@user . On the second page my session data is lost (Session::all() is empty). What could be causing this issue?

Hydroxis
  • 95
  • 1
  • 12

1 Answers1

6

Try wrapping your routes (inside app/Http/routes.php) in a Route::group() with the web middleware:

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

An easy way to test this:

Route::group(['middleware' => 'web'], function () {
    Route::get('', function () {
        Session::set('test', 'testing');
    });

    Route::get('other', function () {
        dd(Session::get('test'));
    });
});

If you remove the web middleware, you'll receive null since the web middleware is responsible for starting the session.

Ensure you have the web middleware group inside your app/Http/Kernel.php:

protected $middlewareGroups = [
    'web' => [
        Middleware\EncryptCookies::class,
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
        \Illuminate\Session\Middleware\StartSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        Middleware\VerifyCsrfToken::class,
    ],
];
Steve Bauman
  • 8,165
  • 7
  • 40
  • 56
  • Issue still persists. – Hydroxis Feb 01 '16 at 21:15
  • Inside your `app/Http/Kernel.php`, do you have the middleware group `web`? – Steve Bauman Feb 01 '16 at 21:18
  • Strange... Change your session driver to `file` and then see if your session is created in `storage/framework/sessions`. You may need to clear the config cache for this to take effect. – Steve Bauman Feb 01 '16 at 21:22
  • Strange. Before I changed to another driver I was using file and the sessions were there. Now they aren't. However when I access / it dumps the correct data. Wtf? (I did clear cache) – Hydroxis Feb 01 '16 at 21:23
  • Yea that's definitely odd. So it works though? If not, try clearing all caches (`route:clear, cache:clear, config:clear`) – Steve Bauman Feb 01 '16 at 21:29
  • It doesn't work, the session data is displayed at / but lost at /user ( I did clear all caches) – Hydroxis Feb 01 '16 at 21:30