繁体   English   中英

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

[英]Auth::user() returns NULL after redirect

我正在尝试登录我的 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加载了这个错误

试图获取非对象的属性

然后,我在我的dashboard.blade.php检查我的Auth::user()

它返回NULL

为什么它突然返回null

我应该检查什么? 会话 / 身份验证 / Apache / Laravel ?

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM