Laravel 5.3 密码代理自定义

Laravel 5.3 Password Broker Customization(Laravel 5.3 密码代理自定义)
本文介绍了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 密码代理自定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

    本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

    相关文档推荐

    Convert JSON integers and floats to strings(将JSON整数和浮点数转换为字符串)
    in php how do I use preg replace to turn a url into a tinyurl(在php中,如何使用preg替换将URL转换为TinyURL)
    all day appointment for ics calendar file wont work(ICS日历文件的全天约会不起作用)
    trim function is giving unexpected values php(Trim函数提供了意外的值php)
    Basic PDO connection to MySQL(到MySQL的基本PDO连接)
    PHP number_format returns 1.00(Php number_Format返回1.00)