当前位置:Gxlcms > PHP教程 > 这个laravel的语法错误怎么写才对?

这个laravel的语法错误怎么写才对?

时间:2021-07-01 10:21:17 帮助过:4人阅读

用laravel5.2自带的注册表单,给它添加一个验证码项,和原表单一起提交验证,控制器的验证方法原来是这样的:

protected function validator(array $data)
{
    return Validator::make($data, [
        'name' => 'required|max:255',
        'email' => 'required|email|max:255|unique:users',
        'password' => 'required|confirmed|min:6',        
    ]);
}

因为增加了一项验证码,所以变成这样:
1、加了一个Request $request参数;
2、加了一行代码$verification_code_session = $request->session()->get('verification_code', '');)

protected function validator(array $data,Request $request)
{
    $verification_code_session = $request->session()->get('verification_code', '');

    return Validator::make($data, [
        'name' => 'required|max:255',
        'email' => 'required|email|max:255|unique:users',
        'password' => 'required|confirmed|min:6',
        'verification_code' => 'required|in:' . $verification_code_session
    ]);

错误信息如下:

Type error: Argument 2 passed to App\Http\Controllers\Auth\AuthController::validator() must be an instance of Illuminate\Http\Request, none given, called in D:\wnmp\www\laravel-entrust\vendor\laravel\framework\src\Illuminate\Foundation\Auth\RegistersUsers.php on line 69

总之就是参数语法不对之类的问题,不知道语法怎么写,大神帮看一下。

回复内容:

用laravel5.2自带的注册表单,给它添加一个验证码项,和原表单一起提交验证,控制器的验证方法原来是这样的:

protected function validator(array $data)
{
    return Validator::make($data, [
        'name' => 'required|max:255',
        'email' => 'required|email|max:255|unique:users',
        'password' => 'required|confirmed|min:6',        
    ]);
}

因为增加了一项验证码,所以变成这样:
1、加了一个Request $request参数;
2、加了一行代码$verification_code_session = $request->session()->get('verification_code', '');)

protected function validator(array $data,Request $request)
{
    $verification_code_session = $request->session()->get('verification_code', '');

    return Validator::make($data, [
        'name' => 'required|max:255',
        'email' => 'required|email|max:255|unique:users',
        'password' => 'required|confirmed|min:6',
        'verification_code' => 'required|in:' . $verification_code_session
    ]);

错误信息如下:

Type error: Argument 2 passed to App\Http\Controllers\Auth\AuthController::validator() must be an instance of Illuminate\Http\Request, none given, called in D:\wnmp\www\laravel-entrust\vendor\laravel\framework\src\Illuminate\Foundation\Auth\RegistersUsers.php on line 69

总之就是参数语法不对之类的问题,不知道语法怎么写,大神帮看一下。

不要用依赖注入的方式注入Request类

直接这样

$verification_code_session = Request::session()->get('verification_code', '');

手机撰写,理解意思就行

除了可以静态调用Request::session()(Facade)以及直接调用request()->session()(helper)外,如果一定要用依赖注入的话,你必须把需要依赖注入的参数放到括号的前边,像下边这样:

protected function validator(Request $request, array $data)
{
    //...
}

人气教程排行