在Laravel 5.2中尝试手动身份验证时出现奇怪错误


Strange error while trying manual authentication in 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', 'ProfilesController@login');

这是我的登录方法:

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

我的表是"users",它有"username"answers"password"你知道怎么解决这个问题吗?注意:我添加了使用Auth;在我的控制器顶部

您的App'User类必须实现Illuminate'Contracts'Auth'Authenticable合约。这是因为repository方法具有这样的类型提示。默认的用户实现(至少现在是5.2)通过扩展Illuminate'Foundation'Auth'User间接地做到了这一点。

您可以在有关身份验证的文档条目中找到有关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
{
    // ...
}