如何扩展 Laravel 的 bcrypt 方法

How to extend Laravel's bcrypt method

我做了一个php class that combines different hash algorithms,我想在bcrypt() laravel的方法中实现它。

我目前的解决方案是访问AuthController并将bcrypt($data['password'])替换为bcrypt(phashp($data['password'])),但我想知道是否有一种方法可以在不更改Illuminate Hashing中的代码的情况下修改方法供应商也不在 AuthController 中。

如何扩展此方法?

谢谢!

您需要做的是进入 config/app.php 并将 Illuminate\Hashing\HashServiceProvider::class, 替换为自定义单例,您现在可以设置自定义单例。在上面的提供者中有:

$this->app->singleton('hash', function () {
    return new BcryptHasher;
});

你可以这样做:

$this->app->singleton('hash', function () {
    return new MyCustomHasher;
});

当然还要定义 MyCustomHasher class 来实现 HasherContract 接口

它应该可以正常工作,因为当您查看 bcrypt 定义时:

function bcrypt($value, $options = [])
{
    return app('hash')->make($value, $options);
}

你看到你 运行 最后 运行 class 绑定到 hash