doubang4881 2014-03-30 04:16
浏览 108

php - Laravel Auth和Session无法正常工作

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
  • 写回答

1条回答 默认 最新

  • dsbqfrr098575666 2014-03-30 13:58
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线