2016-07-12 170 views
0

在我的laravel應用程序中,我執行了php artisan make:auth並創建了所有文件以使身份驗證正常工作。Laravel 5.2登錄不會持續

我AuthController.php代碼:

 /** 
    * Where to redirect users after login/registration. 
    * 
    * @var string 
    */ 
    protected $redirectTo = '/'; 
    protected $loginPath = '/login'; 
    protected $username = 'username'; 

    /** 
    * Create a new authentication controller instance. 
    * 
    * @return void 
    */ 
    public function __construct() 
    { 
     $this->middleware($this->guestMiddleware(), ['except' => 'logout']); 
    } 

    /** 
    * Get a validator for an incoming registration request. 
    * 
    * @param array $data 
    * @return \Illuminate\Contracts\Validation\Validator 
    */ 
    protected function validator(array $data) 
    { 
     return Validator::make($data, [ 
      'username' => 'required|max:255|unique:users', 
      'email' => 'required|email|max:255|unique:users', 
      'password' => 'required|min:6|confirmed', 
     ]); 
    } 

    /** 
    * Create a new user instance after a valid registration. 
    * 
    * @param array $data 
    * @return User 
    */ 
    protected function create(array $data) 
    { 
     return User::create([ 
      'username' => $data['username'], 
      'email' => $data['email'], 
      'password' => bcrypt($data['password']), 
      'hash-confirm' => md5($data['username']+Carbon::now()->resetToStringFormat()), 
     ]); 
    } 
} 

我user.php的:

/** 
    * The attributes that are mass assignable. 
    * 
    * @var array 
    */ 
    protected $fillable = [ 
     'username', 'email', 'password', 'hash-confirm', 
    ]; 

    /** 
    * The attributes that should be hidden for arrays. 
    * 
    * @var array 
    */ 
    protected $hidden = [ 
     'password', 'remember_token', 
    ]; 

我routes.php文件:

Route::auth(); 
Route::get('/', '[email protected]'); 
Route::get('create', '[email protected]'); 

的事情是,如果我把用戶名和密碼錯誤它告訴我這是錯誤的,當它是正確的重定向我,但當我登錄後到達頁面時,Auth :: guest()是true並且Auth :: user()是空的,所以會話不會被創建。 我使用最新版本的laravel,所以我不需要在路由上放置中間件「web」組。

謝謝。

+0

登錄後,「return \ Auth :: user();」是什麼?在你的索引方法(家庭控制器)返回? –

+0

確實返回用戶登錄。 '$ lang = substr(Request :: server('HTTP_ACCEPT_LANGUAGE'),0,2); App :: setLocale($ lang); ('index') - >('menu',false);' 這就是索引方法的內容。執行此操作後,它已經登錄了。 – markerstone

+0

你想用上面的代碼做什麼? –

回答

0

對於那些有同樣的問題更改用戶表的「ID」字段設置爲:

$table->increments("id"); 

我不得不$table->string("id")能夠生成ID自己並沒有持續登錄。