Laravel 5.3 密码代理自定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40532296/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5.3 Password Broker Customization
提问by Andre F.
Does anyone know how to override the functions used within laravel's password broker? I know the docs:
有谁知道如何覆盖 laravel 的密码代理中使用的功能?我知道文档:
https://laravel.com/docs/5.3/passwords#resetting-views
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.
我已经知道如何覆盖ResetsPasswords.php
Trait 但覆盖Password::broker()
下一层的功能。
If there is more information needed I can kindly provide some.
如果需要更多信息,我可以提供一些。
Thank you in advance.
先感谢您。
回答by KoKa
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:
我不得不面对同样的问题,需要覆盖一些 PasswordBroker 功能。经过在网络上的大量调查和许多失败的尝试之后,我最终实现了以下实现:
Created a CustomPasswordResetServiceProviderinside App\Providers where I registered a CustomPasswordBrokerManagerinstance.
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.phpcommented out line:
//Illuminate\Auth\Passwords\PasswordResetServiceProvider::class,
and added:App\Providers\CustomPasswordResetServiceProvider::class,
Inside App\Services folder created a CustomPasswordBrokerManagerand copied the context of the default PasswordBrokerManagerlocated at:
Illuminate\Auth\Passwords\PasswordBrokerManager.php
Then modified the function resolveto return an instance of my CustomPasswordProviderclass.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 CustomPasswordBrokerclass which extends default PasswordBrokerlocated 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 }
在 App\Providers 中创建了一个CustomPasswordResetServiceProvider,我在其中注册了一个CustomPasswordBrokerManager实例。
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注释掉 line:
//Illuminate\Auth\Passwords\PasswordResetServiceProvider::class,
并添加:App\Providers\CustomPasswordResetServiceProvider::class,
在 App\Services 文件夹中创建了一个CustomPasswordBrokerManager并复制了位于以下位置的默认PasswordBrokerManager的上下文:
Illuminate\Auth\Passwords\PasswordBrokerManager.php
然后修改了函数resolve以返回我的CustomPasswordProvider类的一个实例。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:
Illuminate\Auth\Passwords\PasswordBroker 并覆盖了我需要的功能。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.
不确定这是否是最好的实现,但它对我有用。
回答by Adam
There are some missing things for step 1 & 3 in the answer https://stackoverflow.com/a/42855948/2311074
答案中的第 1 步和第 3 步缺少一些内容https://stackoverflow.com/a/42855948/2311074
Step 1
第1步
Probably the safest way is to simply copy the class from Illuminate\Auth\Passwords\PassswordResetServiceProvider.php
to App\Provider\CustomPasswordResetServiceProvider
and change:
可能最安全的方法是简单地将类从Illuminate\Auth\Passwords\PassswordResetServiceProvider.php
to复制App\Provider\CustomPasswordResetServiceProvider
并更改:
- Namespace to
namespace App\Providers;
- Class name to
CustomPasswordResetServiceProvider
- Add
use App\Services\CustomPasswordBrokerManager;
to the top - Inside the function
registerPasswordBroker
renamePasswordBrokerManager
toCustomPasswordBrokerManager
- 命名空间
namespace App\Providers;
- 班级名称
CustomPasswordResetServiceProvider
- 添加
use App\Services\CustomPasswordBrokerManager;
到顶部 - 在函数内部
registerPasswordBroker
重命名PasswordBrokerManager
为CustomPasswordBrokerManager
Step 2.
第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
- 将命名空间更改为
namespace App\Services;
- 添加
use Illuminate\Auth\Passwords\DatabaseTokenRepository;
到顶部 - 将班级名称更改为
CustomPasswordBrokerManager