2017-07-24 76 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