0

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
12
  • can you write the routes.php? Commented Mar 30, 2014 at 5:41
  • what settings do you have listed in your config for sessions? You are probably trying to use a session that does not exist on your webserver (i.e. memcache or something) Commented Mar 30, 2014 at 6:02
  • @angoru: what do you mean by that? Commented Mar 30, 2014 at 6:55
  • @TheShiftExchange: i left everything in config/sessions.php unchanged with the default settings. Commented Mar 30, 2014 at 6:56
  • 1
    You should be using return instead of echo, not sure if that'll solve the problem though. Commented Mar 30, 2014 at 8:11

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

again, apologies that's just a typo when typing it in here, i do have return in my code. it's functional. i've updated the code block above to show it now.

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.