我对这个框架还很陌生,它对我来说太神奇了。
我甚至找不到它在route和controller中调用reset()函数的位置。
但我知道在浏览了一整天的谷歌之后,在controller面前打过电话。
问题是,
我一直在测试用passwordbroker中的默认值覆盖函数reset和函数validatePasswordWithDefaults
我是通过扩展passwordbroker来实现的,但似乎我必须将illuminate\auth\passwords\passwordbroker中的所有函数完全迁移到
app\services\passwordbroker否则我将遇到错误:
Target [Illuminate\Contracts\Auth\UserProvider] is not instantiable
我的示例代码如下:
自定义PasswordServiceProviders,用于绑定PasswordBroker以照亮PasswordBroker:
<?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;
}
}
?>
最佳答案
这并不简单,在我看来,框架不应该向代码发送如此深入的电子邮件,并提供一种从控制器重写它的方法。
我必须重写电子邮件发送,因为我需要使用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数组中注册新的提供程序。注释掉默认值(illumed\auth\passwords\passwordresetserviceprovider)