【问题标题】:password reset table column name override laravel 5密码重置表列名覆盖 laravel 5
【发布时间】:2023-03-27 21:34:01
【问题描述】:

您好朋友,我已修改密码重置表列名“created_at”而不是“created”。如果我在迁移时更改列名“created”,但我收到错误列未找到“created_at”。

 \vendor\laravel\framework\src\Illuminate\Auth\Passwords\DatabaseTokenRepository.php
 protected function getPayload($email, $token)
{
    return ['email' => $email, 'token' => $token, 'created_at' => new Carbon];
}

这是来自列名“created_at”的文件,我可以在其中覆盖此函数,请建议我..

【问题讨论】:

    标签: php laravel laravel-5


    【解决方案1】:

    我想我已经找到了一种无需接触供应商目录的方法。

    对于 Laravel 5.2

    1. 创建一个扩展Illuminate\Auth\Passwords\DatabaseTokenRepository的类
    2. 覆盖那里的 getPayload() 方法
    3. 创建一个扩展Illuminate\Auth\Passwords\PasswordBrokerManager的类
    4. 覆盖 resolve() 方法以使用您在步骤 1 中的令牌存储库返回新的 PasswordBroker
    5. 打开 config/app.php 并从 providers 数组中注释掉 PasswordResetServiceProvider
    6. 在您的应用服务提供商中,从第 3 步注册您的密码代理管理器实例

      $this->app->singleton('auth.password', function ($app) { 返回新的 YourPasswordBrokerManager($app); });

      $this->app->bind('auth.password.broker', function ($app) { return $app->make('auth.password')->broker(); });

    对于 Lravel 5

    1. 创建一个扩展Illuminate\Auth\Passwords\DatabaseTokenRepository的类
    2. 覆盖那里的 getPayload() 方法
    3. 创建一个扩展Illuminate\Auth\Passwords\PasswordResetServiceProvider的类
    4. 覆盖 registerTokenRepository() 以从步骤 1 返回您的存储库
    5. 打开 config/app.php 并从 providers 数组中注释掉 PasswordResetServiceProvider
    6. 将步骤 3 中的提供程序添加到提供程序数组中

    请注意,我尚未对此进行测试,但理论上应该可行。

    【讨论】:

    • 我使用的是 laravel 5 Illuminate\Auth\Passwords\PasswordBrokerManager 不存在。我可以在哪里获得 resove() 方法。谢谢
    • @MGS 我的错误,这种方法适用于 5.2。请检查我上面的版本 5 的编辑。
    • 你能解释一下第 6 步我需要做什么吗.. 谢谢你的回复
    • @MGS 来自您在第 5 步中注释掉的同一提供者数组,添加您在第 3 步中创建的类。
    【解决方案2】:

    已解决:我在 Laravel 5.4 项目中应用了 5.2 步骤:

    ForgotPasswordHelperRepository.php

     <?PHP
    
    
     namespace App\Helpers;
    
    
    use Carbon\Carbon;
    
     class ForgotPasswordHelperRepository extends \Illuminate\Auth\Passwords\DatabaseTokenRepository
       {
    
    /**
     * Build the record payload for the table.
     * I wanted to add an extra column organization_id
     * organizationId() is a helper method I created
     * @param string $email
     * @param string $token
     * @return array
     */
    protected function getPayload($email, $token)
    {
        return ['email' => $email, 'token' => $this->hasher->make($token), 'created_at' => new Carbon,'organization_id' => organizationId()];
    }
    
      }
    

    PasswordBrokerManagerHeler.php

    <?PHP
    
    
    namespace App\Helpers;
    
    
    use Closure;
    use Illuminate\Auth\Passwords\DatabaseTokenRepository;
    use Illuminate\Auth\Passwords\PasswordBroker;
    use Illuminate\Support\Str;
    
     class PasswordBrokerManagerHelper extends \Illuminate\Auth\Passwords\PasswordBrokerManager
    {
    
    /**
     * @inheritDoc
     */
    public function sendResetLink(array $credentials)
    {
        // TODO: Implement sendResetLink() method.
    }
    
    /**
     * @inheritDoc
     */
    public function reset(array $credentials, Closure $callback)
    {
        // TODO: Implement reset() method.
    }
    
    /**
     * @inheritDoc
     */
    public function validator(Closure $callback)
    {
        // TODO: Implement validator() method.
    }
    
    /**
     * @inheritDoc
     */
    public function validateNewPassword(array $credentials)
    {
        // TODO: Implement validateNewPassword() method.
    }
    
    /**
     * Resolve the given broker.
     *
     * @param  string  $name
     * @return \Illuminate\Contracts\Auth\PasswordBroker
     *
     * @throws \InvalidArgumentException
     */
    protected function resolve($name)
    {
    
        $config = $this->getConfig($name);
    
        if (is_null($config)) {
            throw new \InvalidArgumentException("Password resetter [{$name}] is not defined.");
        }
    
        // The password broker uses a token repository to validate tokens and send user
        // password e-mails, as well as validating that password reset process as an
        // aggregate service of sorts providing a convenient interface for resets.
        return new PasswordBroker(
            $this->createTokenRepository($config),
            $this->app['auth']->createUserProvider($config['provider'])
        );
    }
    
    
    
    protected function createTokenRepository(array $config)
    {
        $key = $this->app['config']['app.key'];
    
        if (Str::startsWith($key, 'base64:')) {
            $key = base64_decode(substr($key, 7));
        }
    
        $connection = isset($config['connection']) ? $config['connection'] : null;
    
    //        return new DatabaseTokenRepository(
        return new ForgotPasswordHelperRepository(
            $this->app['db']->connection($connection),
            $this->app['hash'],
            $config['table'],
            $key,
            $config['expire']
        );
    }
    }
    

    接下来,只需将以下内容复制并粘贴到 AppServiceProvider@register 方法中

      $this->app->singleton('auth.password', function ($app) { return new    PasswordBrokerManagerHelper($app); });
      $this->app->bind('auth.password.broker', function ($app) { return $app->make('auth.password')->broker(); });
    

    【讨论】:

      猜你喜欢
      • 2015-05-27
      • 2017-01-22
      • 2019-03-26
      • 2015-09-09
      • 1970-01-01
      • 2017-02-24
      • 2016-01-05
      • 2015-07-01
      • 2019-01-12
      相关资源
      最近更新 更多