Laravel 5.3 Password Broker Customization

元气小坏坏 提交于 2019-11-30 12:12:26

问题


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.


回答1:


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 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'];
        }
    }
    
  2. In config/app.php commented out line:
    //Illuminate\Auth\Passwords\PasswordResetServiceProvider::class,
    and added:
    App\Providers\CustomPasswordResetServiceProvider::class,

  3. 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'])
    );
    }
    
  4. 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.




回答2:


There are some missing things for step 1 & 3 in the answer https://stackoverflow.com/a/42855948/2311074

Step 1

Probably the safest way is to simply copy the class from Illuminate\Auth\Passwords\PassswordResetServiceProvider.php to App\Provider\CustomPasswordResetServiceProviderand change:

  1. Namespace to namespace App\Providers;
  2. Class name to CustomPasswordResetServiceProvider
  3. Add use App\Services\CustomPasswordBrokerManager; to the top
  4. Inside the function registerPasswordBroker rename PasswordBrokerManager to CustomPasswordBrokerManager

Step 2.

Besides changing the resolve method also do the following:

  1. Change namespace to namespace App\Services;
  2. Add use Illuminate\Auth\Passwords\DatabaseTokenRepository; to the top
  3. Change class name to CustomPasswordBrokerManager


来源:https://stackoverflow.com/questions/40532296/laravel-5-3-password-broker-customization

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!