Vuk Stanković
Vuk Stanković

Reputation: 7954

Session not working in Laravel 4

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');

Upvotes: 5

Views: 9127

Answers (6)

user889030
user889030

Reputation: 4754

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,

Upvotes: 0

namal
namal

Reputation: 1274

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)

Upvotes: 0

Lionel Morrison
Lionel Morrison

Reputation: 596

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());
}

Upvotes: 5

philipbrown
philipbrown

Reputation: 554

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

Upvotes: 2

Vuk Stanković
Vuk Stanković

Reputation: 7954

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

Upvotes: 4

lowerends
lowerends

Reputation: 5267

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.

Upvotes: 0

Related Questions