2016-11-07 44 views
0

我試圖登錄到我的Laravel應用程序,並且我遇到了一些我似乎不知道爲什麼的奇怪問題。Auth :: user()在重定向後返回NULL

我有一個函數

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":"[email protected]","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

這似乎是一個會話問題。您是否使用單獨的前端或刀片視圖? – schellingerht

+0

我正在使用刀片視圖。我如何檢查或測試我的會話? – ihue

+0

你有哪個Laravel 4.xxx版本? –

回答

-2

你可以使用Auth :: user() - > username來獲取當前用戶的用戶名值。

+0

你在說什麼男人?你讀過我的文章了嗎? – ihue

+0

我說你不能在你的視圖中獲得Auth :: user()的價值,是一個objet。你可以訪問所有屬性,看看它是否設置。 –

+0

@AxelBruneaux你仍然不明白這個問題:Auth:user()返回** null **無論哪裏被調用。您可以在視圖中使用Auth :: user()。 –

相關問題