我已经成功实施了Laravel的注册,登录和注销Auth。但是,我在解决以下错误时遇到了很多困难,试图实现密码重置功能。</ p>
Error: User must implement CanResetPassword interface.
我相信我的User类配置正确:
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
class User extends Model implements AuthenticatableContract,
CanResetPasswordContract {
use Authenticatable, CanResetPassword;
...
}
我没有使用Eloquent,所以我在config / auth.php中有以下内容:
'driver' => 'database'
有没有人知道我为什么会收到“用户必须实现CanResetPassword接口”错误?
非常感谢!
编辑:添加堆栈跟踪:
UnexpectedValueException in PasswordBroker.php line 237:
User must implement CanResetPassword interface .
in PasswordBroker.php line 237
at PasswordBroker->getUser( array('email' => 'admin2@beaudini.com')) in PasswordBroker.php line 81
at PasswordBroker->sendResetLink (array('email' => 'admin2@beaudini.com'), object(Closure)) in Facade .php line 217
at Facade::__callStatic('sendResetLink' , array(array('email' => 'admin2@beaudini.com'), object(Closure))) in ResetsPasswords.php line 35
at Password::sendResetLink(array ('email' => 'admin2@beaudini.com'), object(Closure)) in ResetsPasswords .php line 35
at PasswordController->postEmail (object(Request))
at call_user_func_array(array(object(PasswordController), 'postEmail'), array( object(Request))) in Controller.php line 256
at Controller->callAction('postEmail', array(object(Request))) in ControllerDispatcher .php line 164
at ControllerDispatcher->call (object(PasswordController), object(Route), 'postEmail') in ControllerDispatcher .php line 112
at ControllerDispatcher->Illuminate \Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 139
at Pipeline->Illuminate\Pipeline\{closure }(object(Request)) in RedirectIfAuthenticated.php line 41
at RedirectIfAuthenticated->handle(object(Request), object(Closure))
at call_user_func_array(array(object(RedirectIfAuthenticated), 'handle'), array (object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure }(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in ControllerDispatcher.php line 114
at ControllerDispatcher->callWithinStack (object(PasswordController), object(Route), object(Request), 'postEmail') in ControllerDispatcher.php line 69
at ControllerDispatcher->dispatch (object(Route), object(Request), 'App\Http\Controllers\Auth\PasswordController', 'postEmail') in Route.php line 203
at Route->runWithCustomDispatcher(object (Request)) in Route.php line 134
at Route->run(object(Request)) in Router.php line 704
at Router->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 139
at Pipeline->Illuminate\Pipeline\{closure }(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Router.php line 706
at Router->runRouteWithinStack(object< /em>(Route), object(Request)) in Router.php line 671
at Router->dispatchToRoute(object (Request)) in Router.php line 631
at Router->dispatch(object(Request)) in Kernel.php line 236
at Kernel->Illuminate\Foundation\Http \{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 139
at Pipeline->Illuminate\Pipeline\{closure }(object(Request)) in Debugbar.php line 49
at Debugbar->handle(object (Request), object(Closure ))
at call_user_func_array(array(object(Debugbar), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure }(object(Request)) in VerifyCsrfToken.php line 50
at VerifyCsrfToken->handle(object(Request), object(Closure))
at call_user_func_array(array(object(VerifyCsrfToken), 'handle'), array(object (Request), object(Closure ))) in Pipeline .php line 124
at Pipeline->Illuminate\Pipeline\{closure }(object(Request)) in ShareErrorsFromSession .php line 49
at ShareErrorsFromSession ->handle(object(Request), object(Closure))
at call_user_func_array(array(object(ShareErrorsFromSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure }(object(Request)) in StartSession.php line 62< /a>
at StartSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(StartSession), 'handle'), array(object(Request), object(Closure))) in Pipeline .php line 124
at Pipeline->Illuminate\Pipeline\{closure }(object(Request)) in AddQueuedCookiesToResponse .php line 37
at AddQueuedCookiesToResponse ->handle(object(Request), object(Closure))
at call_user_func_array(array(object(AddQueuedCookiesToResponse), 'handle'), array (object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure }(object(Request)) in EncryptCookies.php line 59
at EncryptCookies->handle (object(Request), object(Closure))
at call_user_func_array(array(object(EncryptCookies), 'handle'), array(object(Request), object(Closure))) in Pipeline .php line 124
at Pipeline->Illuminate\Pipeline\{closure }(object(Request)) in CheckForMaintenanceMode .php line 42
at CheckForMaintenanceMode ->handle(object(Request), object(Closure))
at call_user_func_array(array(object(CheckForMaintenanceMode), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure }(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Kernel.php line 122
at Kernel->sendRequestThroughRouter (object(Request)) in Kernel.php line 87
at Kernel->handle(object( Request)) in index.php line 54
答案 0 :(得分:1)
按照堆栈跟踪。
PasswordBroker.php第237行
https://github.com/laravel/framework/blob/5.1/src/Illuminate/Auth/Passwords/PasswordBroker.php#L237
失败,因为DatabaseUserProvider
retrieveByCredentials
会返回GenericUser
个对象,不您的用户类对象。
https://github.com/laravel/framework/blob/5.1/src/Illuminate/Auth/DatabaseUserProvider.php#L99
这意味着,基本上,Laravel附带的密码提醒功能仅由eloquent
auth驱动程序支持。您无法将其与database
驱动程序一起使用。你的选择是
在服务提供者内部(AppServiceProvider,或创建一个新的):
Auth::extend('custom', function($app) {
return new CustomUserProvider($app['database.connection']);
});
可能只是扩展数据库用户提供程序并覆盖getGenericUser
来改为使用您的类。
class CustomUserProvider extends \Illuminate\Auth\DatabaseUserProvider
{
protected function getGenericUser($user) {
if ($user !== null) {
return new \App\User((array) $user);
}
}
}
答案 1 :(得分:1)
我在laravel 5.2中遇到了这个问题,我发现了一个像:
这样的解决方案<?php
namespace App;
use Eloquent;
// use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Auth\Authenticatable as AuthenticableTrait;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\CanResetPassword as
CanResetPasswordContract;
class User extends Eloquent implements
Authenticatable,CanResetPasswordContract
{
use AuthenticableTrait,CanResetPassword;
.......
.......
}
答案 2 :(得分:0)
以下内容适用于您创建的任何模型
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Auth\Authenticatable as AuthenticableTrait;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
class customer extends Model implements AuthenticatableContract,CanResetPasswordContract
{
//protected $primaryKey = 'customerId';
use AuthenticableTrait,CanResetPassword;
use ValidatesRequests;
protected $fillable=['name','email','password','country','favorite','gender','image'];
protected $hidden = [
'password', 'remember_token',
];
}
希望有所帮助