2016-04-29 54 views
1

我是新來的Laravel 5.2所以現在我想手動身份驗證,但我得到這個錯誤:奇怪的錯誤,而在Laravel試圖手動認證5.2

1/1 
ErrorException in EloquentUserProvider.php line 114: 
Argument 1 passed to Illuminate\Auth\EloquentUserProvider::validateCredentials() must be an instance of Illuminate\Contracts\Auth\Authenticatable, instance of App\User given, called in C:\xampp\htdocs\myblog\vendor\laravel\framework\src\Illuminate\Auth\SessionGuard.php on line 378 and defined 

這裏是我的形式:

<form method="post" action='login'> 
       {!! csrf_field() !!} 
        <div class="form-group"> 
        <label for="username">Username</label> 
        <input name="username" placeholder="Your name" class="form-control"> 
        </div> 

        <div class="form-group"> 
        <label for="password">Password</label> 
        <input name="password" class="form-control"> 
        </div> 

        <div class="form-group"> 
        <button class="btn btn-primary" type="submit">Login</button> 
        </div> 

       </form> 

這是我的路線:

Route::post('login', '[email protected]'); 

,這裏是我的登錄方法:

public function login(Request $request){ 

     if (Auth::attempt(['username' => $request->username, 'password' => $request->password])) { 
      // Authentication passed... 
      return redirect()->intended(); 
     }else{ 
      return "Something went wrong"; 
     } 

    } 

我的表是「用戶」,它有'用戶名'和'密碼' 有什麼想法如何解決這個問題? 注意:我添加了使用Auth;在我的控制器頂部

+0

您可以爲您的App \ User類提供類定義嗎? –

+0

<?php namespace App; 使用Illuminate \ Database \ Eloquent \ Model; 類用戶擴展型號 { 公共職能的職位(){ \t回$這個 - >的hasMany( '應用程序\郵報'); } } 這就是我的應用程序\用戶 –

回答

0

您的App\User類必須執行Illuminate\Contracts\Auth\Authenticable合同。這是因爲存儲庫方法有這種類型。默認的用戶實現(至少現在在5.2)通過擴展Illuminate\Foundation\Auth\User間接地實現。

您可以在documentation關於驗證的條目中找到關於Authenticable合同的信息。從該鏈接,該合同是這樣的:

namespace Illuminate\Contracts\Auth; 

interface Authenticatable { 

    public function getAuthIdentifierName(); 
    public function getAuthIdentifier(); 
    public function getAuthPassword(); 
    public function getRememberToken(); 
    public function setRememberToken($value); 
    public function getRememberTokenName(); 

} 

幸運的是,Laravel提供Illuminate\Auth\Authenticable一個特點,它提供了很多這方面的功能,爲你,所以你應該需要做的,以滿足這個錯誤是use該性狀,您可以按照Laravel 5.2默認的方式執行相同的操作(通過繼承同一個類):

<?php namespace App; 

use Illuminate\Foundation\Auth\User as Authenticable; 

class User extends Authenticable 
{ 
    // ... 
} 
+0

偉大的工程! 但我現在有一個簡單的問題,每次輸入正確的用戶名和密碼它返回出錯出錯 當我輸入用戶的密碼我試圖登錄與我使用bcrypt函數,我應該使用bcrybt的密碼給出從形式? –

+0

默認情況下,您不必使用任何哈希函數。參見'Illuminate \ Foundation \ Auth \ AuthenticatesUsers'。這是默認的'應用程序\ Http \控制器\ Auth \ AuthController'類使用的特性 –

+0

偉大的工作,謝謝你這麼多兄弟:) –