我想通过向密码立面和PasswordBroker中添加方法来修改PasswordBroker
class DatabaseTokenRepository implements TokenRepositoryInterface
{
/*I want to add the method, reference to the exists() method*/
public function getRecord(CanResetPasswordContract $user)
{
return (array) $this->getTable()->where(
'email', $user->getEmailForPasswordReset()
)->first();
}
}class PasswordBroker implements PasswordBrokerContract
{
/*I want to add the method*/
public function getTokenRecord(CanResetPasswordContract $user)
{
return $this->tokens->getRecord($user);
}
}因此,我可以使用Password::getTokenRecord($user)获取password_resets表中的数据库记录。
我相信使用Password外观,代码更一致,而不是使用DB:table('password_resets')->where('email', $user->email)->first()。
有办法注册这些方法吗?还是宏?或者扩展DatabaseTokenRepository和PasswordBroker的类,然后告诉密码外观使用我的扩展类?
有什么建议吗?谢谢!
发布于 2022-07-26 07:53:06
感谢相似问题,我解决了这个问题!
这是我的密码:
getTokenRecord()让我使用Password::getTokenRecord($user)namespace App\Facades;
use Illuminate\Auth\Passwords\PasswordBroker as Broker;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
class PasswordBroker extends Broker
{
public function getTokenRecord(CanResetPasswordContract $user)
{
return $this->tokens->getRecord($user);
}
}getRecord()方法调用的getTokenRecord()namespace App\Repositories;
use App\Models\Member;
use Illuminate\Auth\Passwords\DatabaseTokenRepository as DatabaseToken;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
class DatabaseTokenRepository extends DatabaseToken
{
public function getRecord(CanResetPasswordContract $user)
{
return $this->getTable()->where(
'email',
$user->getEmailForPasswordReset()
)->first();
}
}PasswordBroker和DatabaseTokenRepository的自定义PasswordBroker<?php
namespace App\Facades;
use App\Facades\PasswordBroker;
use App\Repositories\DatabaseTokenRepository;
use Illuminate\Auth\Passwords\PasswordBrokerManager as Manager;
use InvalidArgumentException;
class PasswordBrokerManager extends Manager
{
/**
* Resolve the given broker.
*
* @param string $name
* @return \Illuminate\Contracts\Auth\PasswordBroker
*
* @throws \InvalidArgumentException
*/
protected function resolve($name)
{
$config = $this->getConfig($name);
if (is_null($config)) {
throw new InvalidArgumentException("Password resetter [{$name}] is not defined.");
}
// 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 PasswordBroker(
$this->createTokenRepository($config),
$this->app['auth']->createUserProvider($config['provider'] ?? null)
);
}
/**
* Create a token repository instance based on the given configuration.
*
* @param array $config
* @return \Illuminate\Auth\Passwords\TokenRepositoryInterface
*/
protected function createTokenRepository(array $config)
{
$key = $this->app['config']['app.key'];
if (str_starts_with($key, 'base64:')) {
$key = base64_decode(substr($key, 7));
}
$connection = $config['connection'] ?? null;
return new DatabaseTokenRepository(
$this->app['db']->connection($connection),
$this->app['hash'],
$config['table'],
$key,
$config['expire'],
$config['throttle'] ?? 0
);
}
}<?php
namespace App\Providers;
use App\Facades\PasswordBrokerManager;
use Illuminate\Support\ServiceProvider;
use Illuminate\Contracts\Support\DeferrableProvider;
class PasswordResetServiceProvider extends ServiceProvider implements DeferrableProvider
{
public function register()
{
$this->registerPasswordBrokerManager();
}
protected function registerPasswordBrokerManager()
{
$this->app->singleton('auth.password', function ($app) {
return new PasswordBrokerManager($app);
});
}
public function provides()
{
return ['auth.password'];
}
}config/app.php的源config/app.php,并添加我的自定义类'providers' => [
// Illuminate\Auth\Passwords\PasswordResetServiceProvider::class,
App\Providers\PasswordResetServiceProvider::class,
]https://stackoverflow.com/questions/73074307
复制相似问题