我对这个框架很陌生,对我来说这似乎太神奇了。 我甚至找不到它在路由和控制器中调用函数reset()的位置。 但是我知道在浏览谷歌一整天之后,它已经在控制器之前调用了。
这是问题所在, 我一直在测试覆盖函数重置和PasswordBroker中的validatePasswordWithDefaults函数
我通过扩展PasswordBroker来做,但似乎我必须将Illuminate \ Auth \ Passwords \ PasswordBroker中的所有功能完全迁移到我的 App \ Services \ PasswordBroker否则我会遇到错误:
Target [Illuminate\Contracts\Auth\UserProvider] is not instantiable
我的示例代码在这里:
将我的PasswordBroker绑定到Illuminate PasswordBroker的自定义PasswordServiceProviders:
<?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;
}
}
?>
答案 0 :(得分:3)
这不是直截了当的,我认为框架不应该在代码中发送如此深入的电子邮件,并提供从控制器覆盖它的方法。
我不得不覆盖电子邮件发送,因为我需要使用Mandrill的api,因此我必须在邮件发送的瞬间发送额外的标头。这就是我所做的:
在App \ Providers \ PasswordResetServiceProvider上创建提供程序类。我复制了框架的默认提供程序(Illuminate \ Auth \ Passwords \ PasswordResetServiceProvider),但我必须对注册顺序稍作修改,稍后才能检索令牌服务。你还必须指定你的PasswordBroker的位置(在我的情况下是在\ App \ Services \ PasswordBroker上)
use Illuminate\Support\ServiceProvider;
use Illuminate\Auth\Passwords\DatabaseTokenRepository 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 \App\Services\PasswordBroker(
$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'];
}
}
创建类\ App \ Services \ PasswordBroker,你可以覆盖emailResetLink(),这一步没有神秘感。
在config \ app.php(App \ Providers \ PasswordResetServiceProvider)中的providers数组上注册新的提供程序。注释掉默认值(Illuminate \ Auth \ Passwords \ PasswordResetServiceProvider)