問題描述
有誰知道如何覆蓋 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
提供有關(guān)如何處理視圖和一些表面級別的事情的信息,但實際上根本不清楚,或者我可能沒有閱讀足夠的時間.
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.
我已經(jīng)知道如何覆蓋 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 函數(shù).經(jīng)過在網(wǎng)絡(luò)上的大量調(diào)查和許多失敗的嘗試,我最終實現(xiàn)了以下實現(xiàn):
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 中創(chuàng)建了一個 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 文件夾創(chuàng)建了一個 CustomPasswordBrokerManager 并復(fù)制了位于以下位置的默認 PasswordBrokerManager 的上下文:
IlluminateAuthPasswordsPasswordBrokerManager.php
然后修改函數(shù) 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 文件夾中,我創(chuàng)建了一個 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
}
不確定這是否是最好的實現(xiàn),但它對我有用.
Not sure if this is the best implementation but it worked for me.
這篇關(guān)于Laravel 5.3 密碼代理自定義的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!