carr0lls
carr0lls

Reputation: 7

php - Laravel Auth and Session not working

The following code prints "test: bar" but not "home: bar". This code works fine locally on my XAMPP server but doesn't work on my webhost server. At first I thought it was an issue with the PHP version, but I don't think it's that anymore. I'm thinking it has to be some setting I have to enable on the webhost? Does anyone know what may be causing this?

class HomeController extends BaseController {

    public function home()
    {
        echo "home: " . Session::get('foo');
    }

    public function test()
    {
        Session::put('foo','bar');
        echo "test: " . Session::get('foo');
        return Redirect::route('home');
    }

}

Actual Output:
test: bar
home:

Expected Output:
test: bar
home: bar

Upvotes: 0

Views: 997

Answers (1)

Jason Lewis
Jason Lewis

Reputation: 18665

Laravel expects a response to be returned from a route (and controllers). Because you're not returning the redirect the session is not persisted and the redirect probably isn't occurring. Simply update your methods to return the correct responses:

class HomeController extends BaseController {

    public function home()
    {
        return "home: " . Session::get('foo');
    }

    public function test()
    {
        Session::put('foo','bar');

        return Redirect::route('home');
    }

}

When you're returning a redirect there's no point in echoing as the response will not be seen anyway.

Upvotes: 1

Related Questions