2017-07-24 71 views
0

我有自定義登錄功能在Laravel 5.4中似乎工作,但不完全。我有UserController.php是會話登錄的用戶返回undefined

public function loginSubmit() 
{ 
    $user = User::where('username', Input::get('username'))->first(); 
    if (!$user) { 
     $validator->messages()->add('username', 'Invalid login or password.'); 
     return Redirect::to('/users/login')->withErrors($validator->errors())->withInput(Input::except(['captcha'])); 
    } 

    if (!Hash::check(Input::get('password'), $user->password)) { 
     $validator->messages()->add('username', 'Invalid login or password.'); 
     return Redirect::to('/users/login')->withErrors($validator->errors())->withInput(Input::except(['captcha'])); 
    } 

    $user->last_login = \Carbon\Carbon::now(); 
    $user->save(); 
    Session::put('user', ['user_id' => $user->user_id]); 
    //dd(Session::get('user', null)); 

    return Redirect::to('/'); 
} 

dd(Session::get('user', null));返回

array:1 [▼ 
    "user_id" => 1 
] 

這意味着,與ID = 1的用戶登錄,並在存儲在會話。在BaseController.php這分享我有這個

public static function isLoggedIn() 
{ 
    $user = Session::get('user', null); 
    if ($user !== null) { 
     return true; 
    } else { 
     return false; 
    } 
} 

用戶會話但是,當我試圖表明的用戶名登錄的用戶

{{ $user->username }} 

我有錯誤

未定義的變量:用戶

這是我的用戶模型

namespace App; 

use Illuminate\Database\Eloquent\Model; 
use Eloquent; 
use DB; 

use Illuminate\Auth\Authenticatable; 
use Illuminate\Auth\Passwords\CanResetPassword; 
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract; 
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract; 

class User extends Eloquent implements AuthenticatableContract, CanResetPasswordContract 
{ 

    use Authenticatable, CanResetPassword; 

    /** 
    * The database table used by the model. 
    * 
    * @var string 
    */ 
    protected $table = 'users'; 

    /** 
    * The attributes excluded from the model's JSON form. 
    * 
    * @var array 
    */ 
    protected $hidden = array('password'); 
    protected $primaryKey = 'user_id'; 
} 

任何想法我的會議在這​​裏有什麼問題嗎?

+0

是否需要爲用戶指定這樣的會話?你可以用'Auth :: user()'來訪問登錄的用戶,或者檢查他們是否登錄了'Auth :: check()'。 – morph

+0

'{{Auth :: user() - > username}}'正在返回'試圖獲取非對象的屬性' – Peter

+1

用'Auth :: login($ user)替換'Session :: put()'' – morph

回答

1

這意味着$user未在您使用的刀片文件中定義。

呼叫與用戶作爲一個參數(我認爲這是最好的方法)的觀點:

$user = Session::get('user', null); 
return view('yourview')->with(['user' => $user]; 

或者在您的刀片文件中使用Session:

{{ Session::get('user', null)->username }} 

編輯:使用View::share。你可以把它放在你的服務提供商的boot功能裏面:

class AppServiceProvider extends ServiceProvider 
    { 
     /** 
     * Bootstrap any application services. 
     * 
     * @return void 
     */ 
     public function boot() 
     { 
      $user = Session::get('user', null); 
      View::share('user', $user); 
     } 
     //... 
    } 
+0

你更喜歡用''來''緊湊',因爲我猜你可以在刀片中使用不同的名稱呢?或者你可以用'compact'來做到這一點嗎? – brianforan

+0

你也可以用'compact'來做。我只是不喜歡在刀片文件中使用會話的想法,因爲如果將用戶對象存儲在變量中,則無需每次訪問會話 – gbalduzzi

+0

但是我在'setupLayout()'函數'View :: share('user',$ user);'應該共享用戶,不是?我現在有點迷路了。 – Peter