dongshan9338 2016-11-10 16:19 采纳率: 0%
浏览 72
已采纳

Laravel 5.3密码代理定制

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.

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.

Thank you in advance.

  • 写回答

2条回答 默认 最新

  • dqp4933 2017-03-17 11:09
    关注

    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. Created a CustomPasswordResetServiceProvider inside App\Providers where I registered a CustomPasswordBrokerManager instance.

      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'];
          }
      }
      
    2. In config/app.php commented out line:
      //Illuminate\Auth\Passwords\PasswordResetServiceProvider::class,
      and added:
      App\Providers\CustomPasswordResetServiceProvider::class,

    3. Inside App\Services folder created a CustomPasswordBrokerManager and copied the context of the default PasswordBrokerManager located at:
      Illuminate\Auth\Passwords\PasswordBrokerManager.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'])
      );
      }
      
    4. Finally inside App\Services folder I created a CustomPasswordBroker class which extends default PasswordBroker located 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    
      }      
      

    Not sure if this is the best implementation but it worked for me.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀