Laravel 5.3 密码代理自定义

时间:2022-11-25
本文介绍了Laravel 5.3 密码代理自定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道如何覆盖 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:

  1. 在 AppProviders 中创建了一个 CustomPasswordResetServiceProvider,我在其中注册了一个 CustomPasswordBrokerManager 实例.

  1. 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.

    这篇关于Laravel 5.3 密码代理自定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

    上一篇:Laravel 动态配置设置 下一篇:Laravel 在用户级别自定义 session.lifetime

    相关文章

    最新文章