laravel 传递给控制器​​的参数 1 必须是 Illuminate\Http\Request?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31641752/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:03:02  来源:igfitidea点击:

Argument 1 passed to controller must be an instance of Illuminate\Http\Request?

phpvalidationlaravel

提问by austin43

I am pretty new to laravel, and have searched everywhere but could not fix this error:

我对 Laravel 还很陌生,到处搜索,但无法修复此错误:

Argument 1 passed to InsertController::insert() must be an instance of Illuminate\Http\Request, none given

传递给 InsertController::insert() 的参数 1 必须是 Illuminate\Http\Request 的实例,没有给出

I am trying to validate my input by passing in the Request method so I don't have to re-write a new validate method for every form, but it seems to always give me this error.

我试图通过传入 Request 方法来验证我的输入,因此我不必为每个表单重新编写新的验证方法,但它似乎总是给我这个错误。

<?php

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;

class InsertController extends Controller {

    public function insert(Request $request) {
        $username = Input::get('username');
        $pw = Hash::make(Input::get('pw'));
        $email = Input::get('email');

        $this->validate($request, ['name' => 'required|unique:users', 
                            'password' => 'required|min:8|max:255',
                            'email' => 'required|email|unique:users']); 


        if(!$validator->fails()) {
            $user = DB::table('users')->insert(
            ['email' => $email, 'password' => $pw, 'name' => $username]);
        }
    }

}

Here is the route I'm calling it with as well.

这也是我调用它的路线。

Route::post('users', ['uses' => 'InsertController@insert', 'before' => 'csrf'], function()
{
    $users = User::all(); //call the User model for all data in users table
    return View::make('users')->with('users', $users);
});

回答by dschniepp

The reason why it is not working is because of the given code relays on the injection of Request-Object, which was introduced with Laravel 5but the installed framework version is 4.2.17.

它不工作的原因是因为给定的代码中继了Request-Object的注入,它是在Laravel 5中引入的,但安装的框架版本是4.2.17

To solve the issue you can remove the Request-Object from the method signature or update Laravel to 5.

要解决此问题,您可以从方法签名中删除Request-Object 或将 Laravel 更新为 5。