Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
386 views
in Technique[技术] by (71.8m points)

php - Laravel 5.3 Password Broker Customization

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.

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.

Thank you in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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. Created a CustomPasswordResetServiceProvider inside AppProviders where I registered a CustomPasswordBrokerManager instance.

    namespace AppProviders;
    use IlluminateSupportServiceProvider;
    use AppServicesCustomPasswordBrokerManager; 
    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'];
        }
    }
    
  2. In config/app.php commented out line:
    //IlluminateAuthPasswordsPasswordResetServiceProvider::class,
    and added:
    AppProvidersCustomPasswordResetServiceProvider::class,

  3. Inside AppServices folder created a CustomPasswordBrokerManager and copied the context of the default PasswordBrokerManager located at:
    IlluminateAuthPasswordsPasswordBrokerManager.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'])
    );
    }
    
  4. Finally inside AppServices folder I created a CustomPasswordBroker class which extends default PasswordBroker located at:
    IlluminateAuthPasswordsPasswordBroker and overridden the functions that I needed.

    use IlluminateAuthPasswordsPasswordBroker 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...