问题描述
有谁知道如何覆盖 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 Trait 但覆盖 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:
在 AppProviders 中创建了一个 CustomPasswordResetServiceProvider,我在其中注册了一个 CustomPasswordBrokerManager 实例.
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']; } }
在 config/app.php 中注释掉一行:
//IlluminateAuthPasswordsPasswordResetServiceProvider::class,
并补充说:
AppProvidersCustomPasswordResetServiceProvider::class,
Inside AppServices 文件夹创建了一个 CustomPasswordBrokerManager 并复制了位于以下位置的默认 PasswordBrokerManager 的上下文:
IlluminateAuthPasswordsPasswordBrokerManager.php
然后修改函数 resolve 以返回我的 CustomPasswordProvider 类的实例.
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']) ); }
最后在 AppServices 文件夹中,我创建了一个 CustomPasswordBroker 类,它扩展了位于以下位置的默认 PasswordBroker:
IlluminateAuthPasswordsPasswordBroker 并覆盖我需要的功能.
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.