我对这个框架真的很陌生,它对我来说太神奇了。我甚至找不到它在路由和控制器中调用reset()函数的位置。但在谷歌上浏览了一整天后,我知道这是控制器前的呼叫。
问题来了,我一直在测试重写函数重置和函数validatePasswordWithDefaults在PasswordBroker
我通过扩展PasswordBroker来做到这一点,但似乎我必须将IlluminateAuthPasswordsPasswordBroker中的所有功能完全迁移到我的AppServicesPasswordBroker否则我将击中错误:
Target [IlluminateContractsAuthUserProvider] is not instantiable
示例代码如下:
自定义PasswordServiceProviders绑定我的PasswordBroker照亮PasswordBroker:
<?php namespace AppProviders;
use IlluminateSupportServiceProvider;
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(
'IlluminateContractsAuthPasswordBroker','AppServicesPasswordBroker'
);
}
}
Custom PasswordBroker:
<?php
namespace AppServices;
use IlluminateContractsAuthUserProvider;
use IlluminateAuthPasswordsTokenRepositoryInterface;
use IlluminateAuthPasswordsPasswordBroker as BasePasswordBroker;
use IlluminateContractsAuthPasswordBroker 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;
}
}
?>
这并不简单,在我看来,框架不应该将电子邮件发送到代码的深处,并提供一种从控制器重写它的方法。
我必须重写电子邮件发送,因为我需要使用Mandrill的api,所以我必须在邮件发送的那一刻发送额外的标题。我是这样做的:
-
在AppProvidersPasswordResetServiceProvider上创建一个provider类。我复制了框架的默认提供程序(IlluminateAuthPasswordsPasswordResetServiceProvider),但我不得不对注册的顺序和后来检索令牌服务的方式做一些轻微的修改。此外,你必须指定你的PasswordBroker在哪里(在我的情况下是在AppServicesPasswordBroker)
use IlluminateSupportServiceProvider; use IlluminateAuthPasswordsDatabaseTokenRepository as DbRepository; class PasswordResetServiceProvider extends ServiceProvider { /** * Indicates if loading of the provider is deferred. * * @var bool */ protected $defer = true; /** * Register the service provider. * * @return void */ public function register() { $this->registerTokenRepository(); $this->registerPasswordBroker(); } /** * Register the password broker instance. * * @return void */ protected function registerPasswordBroker() { return $this->app->singleton('auth.password', function($app) { // The password token repository is responsible for storing the email addresses // and password reset tokens. It will be used to verify the tokens are valid // for the given e-mail addresses. We will resolve an implementation here. $tokens = $app['auth.password.tokens']; $users = $app['auth']->driver()->getProvider(); $view = $app['config']['auth.password.email']; // The password broker uses a token repository to validate tokens and send user // password e-mails, as well as validating that password reset process as an // aggregate service of sorts providing a convenient interface for resets. return new AppServicesPasswordBroker( $tokens, $users, $app['mailer'], $view ); }); } /** * Register the token repository implementation. * * @return void */ protected function registerTokenRepository() { $this->app->singleton('auth.password.tokens', function($app) { $connection = $app['db']->connection(); // The database token repository is an implementation of the token repository // interface, and is responsible for the actual storing of auth tokens and // their e-mail addresses. We will inject this table and hash key to it. $table = $app['config']['auth.password.table']; $key = $app['config']['app.key']; $expire = $app['config']->get('auth.password.expire', 60); return new DbRepository($connection, $table, $key, $expire); }); } /** * Get the services provided by the provider. * * @return array */ public function provides() { return ['auth.password', 'auth.password.tokens']; }
}
-
创建类AppServicesPasswordBroker,在那里你可以重写emailResetLink(),这一步没有什么神秘的
-
从config App .php (App providers PasswordResetServiceProvider)在providers数组中注册新的提供商。注释掉默认的(IlluminateAuthPasswordsPasswordResetServiceProvider)
-