Home > Backend Development > PHP Tutorial > 这个laravel的语法错误怎么写才对?

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

WBOY
Release: 2016-06-06 20:11:24
Original
1026 people have browsed it

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

<code>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',        
    ]);
}</code>
Copy after login
Copy after login

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

<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
    ]);</code>
Copy after login
Copy after login

错误信息如下:

<code>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</code>
Copy after login
Copy after login

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

回复内容:

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

<code>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',        
    ]);
}</code>
Copy after login
Copy after login

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

<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
    ]);</code>
Copy after login
Copy after login

错误信息如下:

<code>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</code>
Copy after login
Copy after login

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

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

直接这样

<code>$verification_code_session = Request::session()->get('verification_code', '');</code>
Copy after login

手机撰写,理解意思就行

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

<code>protected function validator(Request $request, array $data)
{
    //...
}</code>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template