密码重置表列名覆盖laravel 5

时间:2016-11-08 09:46:57

标签: php laravel laravel-5

朋友们,我已经修改了密码重置表列名'created_at'而不是'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'的文件,我可以覆盖此功能请建议我..

2 个答案:

答案 0 :(得分:2)

我想我已经找到了一种方法,可以在不触及供应商目录的情况下完成此操作。

适用于Laravel 5.2

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

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

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

  9. 对于Lravel 5

    1. 创建一个扩展Illuminate\Auth\Passwords\DatabaseTokenRepository
    2. 的类
    3. 覆盖 getPayload()方法
    4. 创建一个扩展Illuminate\Auth\Passwords\PasswordResetServiceProvider
    5. 的类
    6. 覆盖 registerTokenRepository()以从步骤1返回您的存储库
    7. 打开config/app.php并从供应商数组中注释掉 PasswordResetServiceProvider
    8. 将您的提供商从第3步添加到提供商数组
    9. 请注意,我没有对此进行测试,但它应该适用于理论。

答案 1 :(得分:0)

已解决:我在我的 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(); });