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');
6
  • Is getEdit really called before getIndex? Commented Jul 23, 2014 at 10:23
  • Yes. I know this because if News::findOrFail($id) doesn't fail it will load view, and if news is not found it should go to catch. This also works. Tried echoing $e->getMessage() Commented Jul 23, 2014 at 10:26
  • Have you tried Redirect::to('news')->with('message', 'your_message'); ? Commented Jul 23, 2014 at 10:31
  • I tried that first, because I was using that before, but it doesn't work. It doesn't even work when I do Session::put('message', Lang::get('admin.noSuchNews')). And when I dump session with Session::all() I only get csrf token _token Commented Jul 23, 2014 at 10:34
  • Have you configured your session? Commented Jul 23, 2014 at 11:01

6 Answers 6

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());
}
Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

I have followed your solution. But not working for me.
2

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

Comments

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.

1 Comment

I'm using database driver and every request creates a new row there. Some requests even create two rows in session table
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)

Comments

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,

Comments

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.