Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php laravel重置密码自定义验证程序?_Php_Laravel_Laravel 5_Laravel 5.4 - Fatal编程技术网

Php laravel重置密码自定义验证程序?

Php laravel重置密码自定义验证程序?,php,laravel,laravel-5,laravel-5.4,Php,Laravel,Laravel 5,Laravel 5.4,通过查看,我可以看到rules方法受到保护: protected function rules() { return [ 'token' => 'required', 'email' => 'required|email', 'password' => 'required|confirmed|min:6', ]; } 如果我无法覆盖规则方法,那么在lara

通过查看,我可以看到rules方法受到保护:

 protected function rules()
    {
        return [
            'token' => 'required',
            'email' => 'required|email',
            'password' => 'required|confirmed|min:6',
        ];
    }
如果我无法覆盖规则方法,那么在laravel中将另一个自定义验证添加到重置密码表单中的适当方法是什么


我可以覆盖重置方法,但它太大了,如果在将来的版本中,它们会更改一些逻辑,我将遇到麻烦,因为我不想太多地干扰安全功能

Laravel在
App\Http\controllers\auth

其中一个是
ResetPasswordController
控制器,它正在加载trait

在这个类中,您可以只覆盖
rules()
函数

如果不想更新
rules()
函数,则必须更新
reset()
函数来修改数组。但是我不建议这样做,因为这个函数比
rules()
函数更复杂,而且会发生明显的变化

如果要更新特征,可以如下扩展:

trait CustomResetsPasswords {
    use ResetsPasswords;

    public function rules() {
        //return my custom rules
    }
}

然后在
ResetPasswordController
中使用
CustomResetsPasswords
trait。

您可以覆盖
ResetsPasswords
trait中的
reset
方法。因此,它向
Broker
实例添加了一个自定义验证器,并绕过了Laravel无意义的默认验证规则

/**
 * Reset the given user's password.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\RedirectResponse
 */
public function reset(Request $request)
{
    $this->validate($request, $this->rules(), $this->validationErrorMessages());

    // These two lines below allow you to bypass the default validation.
    $broker = $this->broker();
    $broker->validate(function () { return true; });

    $response->reset(
        $this->credentials($request), function ($user, $password) {
            $this->resetPassword($user, $password);
        }
    );

    return $response == Password::PASSWORD_RESET
                ? $this->sendResetResponse($response)
                : $this->sendResetFailedResponse($request, $response);
}
对于laravel 5.8:

在上面的答案中,仅使用trait with custom rules函数是不够的,因为在laravel\framework\src\illumb\Auth\Passwords\PasswordBroker.php文件中,它使用另一种验证方法来检查密码的长度

因此,为了使用您的自定义密码长度,请重写规则和密码代理的验证方法

创建存储密码长度的属性

/**
 * Minimum length required for password
 *
 * @var string
 */
protected $passwordLength = '6';
在app\Http\Controllers\Auth\ResetPasswordController.php中更改构造函数

/**
 * Create a new controller instance.
 *
 * @return void
 */
public function __construct()
{
    $this->middleware('guest');

    $this->broker()->validator(function (array $credentials)
    {
        [$password, $confirm] = [
            $credentials['password'],
            $credentials['password_confirmation'],
        ];

        return $password === $confirm && mb_strlen($password) >= $this->passwordLength;
    });
}
并通过自定义验证添加规则功能

/**
 * Get the password reset validation rules.
 *
 * @return array
 */
protected function rules()
{
    return [
        'token' => 'required',
        'email' => 'required|email',
        'password' => 'required|min:'.$this->passwordLength,
    ];
}
对于Laravel5.8(我没有测试过更新的版本)

最简单的解决方案是从ResetsPasswords属性中调用
规则函数

class ResetPasswordController extends Controller
{

    use ResetsPasswords;

   
    protected $redirectTo = '/home';

    public function __construct()
    {
        $this->middleware('guest');
    }

    // this function is ovverride from ResetsPasswords Traits
    protected function rules()
    {
        return [
            'token' => 'required',
            'email' => 'required|email',
            'password' => 'required|min:8|confirmed',
        ];
    }
}

也许我遗漏了一些内容,但您应该能够覆盖它。默认情况下,它确实有这些内容,在这种情况下不需要生成auth脚手架。我感到困惑,不知何故认为我无法覆盖受保护的方法:/it与命名相混淆。你的意思是最后的;)问题在于,默认验证会覆盖该属性@OzanKurt你找到解决这个问题的方法了吗?似乎覆盖rules方法只是验证ResetsPassword Trait中的验证,而不是PasswordBroker中的验证。