重定向后,Auth :: user()返回NULL

时间:2016-11-07 23:43:38

标签: php laravel laravel-4

我试图登录我的Laravel应用程序,我面临一些奇怪的问题,我似乎不知道为什么。

我有一个功能

public function postLogin(){

    $input = array(
        'username' => Input::get('username'),
        'password' => Input::get('password')
    );

    $rules = array(
        'username'  => 'Required',
        'password'  => 'Required'
    );

    $validator = Validator::make($input, $rules);

    if ($validator->passes()){

        // Try to log the user in.
        if (Auth::attempt($input)){
            $input['status'] = 1;


            //return Auth::user();
            return Redirect::to('/dashboard')->with('success', 'You have logged in successfully');
        }
        else{

            return Redirect::to('/')->withErrors(array('password' => 'Password invalid'))->withInput(Input::except('password'));
        }
    }

    return Redirect::to('/')->withErrors($validator)->withInput(Input::except('password'));
}

当我return Auth::user();时,我的用户打印出来了。

{"id":18,"username":"bheng","email":"benlong@biossusa.com","first_name":"benlong","last_name":"Heng","updated_at":"2016-10-29 15:05:36","created_at":"2014-06-27 19:58:53","status":1,"timezone":"US\/Eastern","lock_version":10,"remember_token":"s2ogIchC7KIfduiFslKwUwW2IYMgcFaDESSUd0UMcPwBYRIxZmpRbqkKfBWt"}

然后,我将线放置

return Redirect::to('/dashboard')->with('success', 'You have logged in successfully');

我重定向到/dashboard页面,dashboard.blade.php加载了此错误

  

尝试获取非对象的属性

然后,我在Auth::user()

中查看我的dashboard.blade.php

返回NULL

问题

为什么它会突然返回null

我应该检查什么? Session / Auth / Apache / Laravel?

如何继续进行调试?

我现在正在接受任何建议。

任何提示/建议/帮助都将非常感谢!

1 个答案:

答案 0 :(得分:-2)

您可以使用Auth :: user() - >用户名来获取当前用户的用户名值。