Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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');
like image 674
Vuk Stanković Avatar asked Jul 23 '14 10:07

Vuk Stanković


People also ask

How session work in Laravel?

Sessions are used to store information about the user across the requests. Laravel provides various drivers like file, cookie, apc, array, Memcached, Redis, and database to handle session data. By default, file driver is used because it is lightweight. Session can be configured in the file stored at config/session.

How do I increase my session lifetime in laravel?

If you want to increase your session life time then we need to change in . env file and it is very easy to change it from configuration file in laravel. laravel provides you session. php file there is we can see 'lifetime' key option for setting time in minutes.


2 Answers

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());
}
like image 129
Lionel Morrison Avatar answered Oct 01 '22 08:10

Lionel Morrison


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

like image 42
Vuk Stanković Avatar answered Oct 01 '22 08:10

Vuk Stanković