5

I have a problem with Session that won't persist.

In controller I have a function that loads a article for editing

public function getEdit($id)
{
    try {
        $news = News::findOrFail($id);
        View::share('title', Lang::get('admin.editNews').": ".$news->title);
        View::share('news', $news);
        $this->layout->content = View::make('news.editNews');
    } catch (Exception $e) {
        Session::flash('message', Lang::get('admin.noSuchNews'));
        Session::flash('notif', 'danger');
        return Redirect::to("news");
    }
}

And I have another function - index, that should display these flash messages.

public function getIndex()
{
    var_dump(Session::get('message'));
}

Session is just not persisting. Not working with Session::flash, not working with Session::put.

Session::get('message') is just always null.

I guess I should mention that I did application routing like this:

Route::controller('news', 'NewsController');
Vuk Stanković
  • 7,864
  • 10
  • 41
  • 65

6 Answers6

5

Check your the return state of the function setting the Session in your controller. Make sure that it returns something even if it's a simple null.

I've just had the same issue and this solved it. I was able to use the following just fine:

Route::get('session', function (){
    Session::put('current_user', 'Lionel Morrison');
    Session::put('user_id', '12345');

    var_dump(Session::all());
});

Route::get('get', function () {
    var_dump(Session::all());
});

but when I used it in a controller like so it didn't work until I returned a null;

public function setsession() {
    Session::put('cat', 'Tom');
    Session::put('mouse', 'Jerry');

    return null;
}

public function getsession() {
    dd(Session::all());
}
Lionel Morrison
  • 566
  • 4
  • 15
4

Ok, I have fixed this.

Thing is, that I have put into session.php file this

'domain' => '.mydomain.com',

But since app is still in localhost, everything with session was failing even though I wasn't using cookie as my Session driver.

When I changed this to

'domain' => '',

Everything started working

Vuk Stanković
  • 7,864
  • 10
  • 41
  • 65
2

If you find that your Session isn't saving correctly, try calling Session::save() explicitly to force it to save.

philipbrown
  • 554
  • 3
  • 8
0

You may want to check the Session settings described at http://laravel.com/docs/session#session-drivers.

The easiest way is to use cookies for sessions (and make sure cookies are enabled in your browser).

If you are using file for storing sessions, make sure the storage path defined in app/config/session.php is writable.

lowerends
  • 5,469
  • 2
  • 24
  • 36
0

If you are tried to login using "Auth::loginUsingId()" api instead of "Auth::attempt()" api, it will destroy the session for another request. So I recommend you, use "cookie" driver instead of "file" of session.php (config\session.php\driver)

namal
  • 1,164
  • 1
  • 10
  • 15
0

for laravel 4.2

goto app/config dir

edit app.php

'url' => 'localhost',

edit session.php

'driver' => 'file',
'cookie' => 'sitename_session',
'domain' => null,
'secure' => false,
user889030
  • 4,353
  • 3
  • 48
  • 51