【发布时间】:2015-07-29 05:18:05
【问题描述】:
我对这个框架真的很陌生,它对我来说似乎很神奇。 我什至找不到它在路由和控制器中调用函数 reset() 的位置。 但我知道在谷歌浏览了一整天之后,它已经在控制器之前被调用了。
问题来了, 我一直在测试在 PasswordBroker 中覆盖函数重置和函数 validatePasswordWithDefaults
我通过扩展 PasswordBroker 来做到这一点,但似乎我必须将 Illuminate\Auth\Passwords\PasswordBroker 中的所有功能完全迁移到我的 App\Services\PasswordBroker 否则我会遇到错误:
Target [Illuminate\Contracts\Auth\UserProvider] is not instantiable
我的示例代码在这里:
将我的 PasswordBroker 绑定到 Illuminate PasswordBroker 的自定义 PasswordServiceProvider:
<?php namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class PasswordResetServiceProvider extends ServiceProvider {
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
//
}
/**
* Register the application services.
*
* @return void
*/
public function register()
{
//
$this->app->bind(
'Illuminate\Contracts\Auth\PasswordBroker','App\Services\PasswordBroker'
);
}
}
Custom PasswordBroker:
<?php
namespace App\Services;
use Illuminate\Contracts\Auth\UserProvider;
use Illuminate\Auth\Passwords\TokenRepositoryInterface;
use Illuminate\Auth\Passwords\PasswordBroker as BasePasswordBroker;
use Illuminate\Contracts\Auth\PasswordBroker as ContractPasswordBroker;
use Closure;
class PasswordBroker extends BasePasswordBroker
{
public function reset(array $credentials, Closure $callback)
{
dd($callback);
$user = $this->validateReset($credentials);
if ( ! $user instanceof CanResetPasswordContract)
{
return $user;
}
$pass = $credentials['password'];
call_user_func($callback, $user, $pass);
$this->tokens->delete($credentials['token']);
return PasswordBrokerContract::PASSWORD_RESET;
}
protected function validatePasswordWithDefaults(array $credentials)
{
list($password, $confirm) = [
$credentials['password'], $credentials['password_confirmation'],
];
return $password === $confirm && mb_strlen($password) >= 4;
}
}
?>
【问题讨论】:
标签: php laravel overriding laravel-5