Laravel 5.3 Password Broker Customization
Solution 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:
-
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']; } }
In config/app.php commented out line:
//Illuminate\Auth\Passwords\PasswordResetServiceProvider::class,
and added:
App\Providers\CustomPasswordResetServiceProvider::class,
-
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']) ); }
-
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.
Solution 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\CustomPasswordResetServiceProvider
and change:
- Namespace to
namespace App\Providers;
- Class name to
CustomPasswordResetServiceProvider
- Add
use App\Services\CustomPasswordBrokerManager;
to the top - Inside the function
registerPasswordBroker
renamePasswordBrokerManager
toCustomPasswordBrokerManager
Step 2.
Besides changing the resolve method also do the following:
- Change namespace to
namespace App\Services;
- Add
use Illuminate\Auth\Passwords\DatabaseTokenRepository;
to the top - Change class name to
CustomPasswordBrokerManager
Andre F.
By Day: Programmer Analyst/U.S Army Blackhawk mechanic By Night: PHP developer/Database Design tinkerer For Fun: Games, Code, Gym and in-depth pondering of what the inter-dimensional multiverse could/can be. ================================================== My Quote "Don't give time your time, for it will take time to pass time. Since we have so little time, there's no time to waste. So make the most of what time you have, today!" -Andre Ferreira
Updated on June 24, 2022Comments
-
Andre F. almost 2 years
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 thePassword::broker()
is for the next layer in.If there is more information needed I can kindly provide some.
Thank you in advance.
-
Andre F. about 7 yearsThis is a very similar implementation that I ended up doing. Very well explained with identification to the directories and all. +1 and marked Correct!
-
johnnydoe82 about 7 yearsJust had to change the required password length for password resets. The min length of 6 characters is burried quite deep in the laravel code so that I could only change it with your answer here. Thank you so much!
-
racl101 almost 7 yearsThank you for this. I was getting stuck on with this Password Broker and Password Broker Manager mess. Seriously, I can't comprehend how they complicated something that used to be simple in such a bad way. Also, for people doing this on Laravel 5.4 there's a slight difference for the
register
method of the service provider than what's shown here. Essentially, theregisterPasswordBroker
method should be copied as is onIlluminate\Auth\Passwords\PasswordResetServiceProvider
except the part where you use your own customCustomPasswordBrokerManager
instance. -
alebupal almost 5 yearsThank you very much, it has helped me a lot!