Laravel 5.3密码代理自定义 [英] Laravel 5.3 Password Broker Customization

查看:106
本文介绍了Laravel 5.3密码代理自定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道如何重写laravel密码代理中使用的功能吗?我知道文档:

Does anyone know how to override the functions used within laravel's password broker? I know the docs:

https://laravel.com/docs/5.3/passwords#resetting-views

提供有关如何处理视图和一些表面级别的内容的信息,但实际上并不清楚,或者我没有足够的时间阅读.

Give information on what to do for things like views and a few surface level things but it's not clear at all really or maybe I'm not reading it enough times.

我已经知道如何覆盖ResetsPasswords.php特性,但是覆盖Password::broker()的功能是针对下一层的.

I already know how to override the ResetsPasswords.php Trait but overriding the functionality of the Password::broker() is for the next layer in.

如果需要更多信息,请提供一些信息.

If there is more information needed I can kindly provide some.

谢谢.

推荐答案

我不得不面对相同的问题,需要重写某些PasswordBroker函数.在网上进行了大量调查并且尝试失败之后,我最终实现了以下实现:

I had to face the same issue, needed to override some of the PasswordBroker functions. After a lot of investigation on the web and many failed attempts to do so, I ended up to the following implementation:

  1. 在我注册了 CustomPasswordBrokerManager 实例的App \ Providers中创建了 CustomPasswordResetServiceProvider .

  1. Created a CustomPasswordResetServiceProvider inside App\Providers where I registered a CustomPasswordBrokerManager instance.

namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Services\CustomPasswordBrokerManager; 
class CustomPasswordResetServiceProvider extends ServiceProvider{
    protected $defer = true;

    public function register()
    {
        $this->registerPasswordBrokerManager();
    }

    protected function registerPasswordBrokerManager()
    {
        $this->app->singleton('auth.password', function ($app) {
            return new CustomPasswordBrokerManager($app);
        });
    }

    public function provides()
    {
        return ['auth.password'];
    }
}

  • config/app.php 中注释了以下行:
    //Illuminate\Auth\Passwords\PasswordResetServiceProvider::class,
    并添加:
    App\Providers\CustomPasswordResetServiceProvider::class,

  • In config/app.php commented out line:
    //Illuminate\Auth\Passwords\PasswordResetServiceProvider::class,
    and added:
    App\Providers\CustomPasswordResetServiceProvider::class,

    内部App \ Services文件夹创建了一个 CustomPasswordBrokerManager ,并复制了位于以下位置的默认 PasswordBrokerManager 的上下文: 照亮\ Auth \ Passwords \ PasswordBrokerManager.php
    然后修改功能 resolve 以返回我的 CustomPasswordProvider 类的实例.

    Inside App\Services folder created a CustomPasswordBrokerManager and copied the context of the default PasswordBrokerManager located at:
    Illuminate\Auth\Passwords\PasswordBrokerManager.php
    Then modified the function resolve to return an instance of my CustomPasswordProvider class.

    protected function resolve($name)
    {
        $config = $this->getConfig($name);
        if (is_null($config)) {
            throw new InvalidArgumentException("Password resetter [{$name}] is not defined.");
        }
    
        return new CustomPasswordBroker(
            $this->createTokenRepository($config),
            $this->app['auth']->createUserProvider($config['provider'])
    );
    }
    

  • 最后在App \ Services文件夹中,我创建了一个 CustomPasswordBroker 类,该类扩展了位于以下位置的默认 PasswordBroker : 照亮\ Auth \ Passwords \ PasswordBroker,并覆盖我需要的功能.

  • Finally inside App\Services folder I created a CustomPasswordBroker class which extends default PasswordBroker located at:
    Illuminate\Auth\Passwords\PasswordBroker and overridden the functions that I needed.

    use Illuminate\Auth\Passwords\PasswordBroker as BasePasswordBroker;    
    
    class CustomPasswordBroker extends BasePasswordBroker    
    {    
    // override the functions that you need here    
    }      
    

  • 不确定这是否是最好的实现,但是对我有用.

    Not sure if this is the best implementation but it worked for me.

    这篇关于Laravel 5.3密码代理自定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆