使用 $input->all() 代替 Input::all() Laravel-5

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31142292/
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 11:51:43  来源:igfitidea点击:

Using $input->all() instead of Input::all() Laravel-5

phplaravellaravel-5

提问by V4n1ll4

I'm trying to use $input->all()as opposed to Input::all()in Laravel-5, however it doesn't seem to like it, even though I am passing the Input reference to the function, like so:

我试图在 Laravel-5 中使用$input->all()而不是Input::all(),但它似乎并不喜欢它,即使我将 Input 引用传递给函数,如下所示:

/**
 * Search for a specified resource.
 *
 * @return Response
 */
public function search(Booking $booking, Input $input)
{
    dd($input->all()); // this doesn't work

    dd(Input::all()); // this DOES work

}

The error I get is:

我得到的错误是:

Call to undefined method Illuminate\Support\Facades\Input::all()

Call to undefined method Illuminate\Support\Facades\Input::all()

Does anyone have a solution to this problem?

有没有人有解决这个问题的方法?

回答by Francesco de Guytenaere

I don't think you're supposed to inject Facades into your Controllers. Inputis a facade for Illuminate\Http\Requestand it's service container binding is request. So according to the documentation, in Laravel 5 you can do Request::all()and in Laravel 5.1 you can do $request->all()

我认为您不应该将 Facades 注入到您的控制器中。Input是一个外观Illuminate\Http\Request,它的服务容器绑定是request。所以根据文档,在 Laravel 5 中你可以做Request::all(),在 Laravel 5.1 中你可以做$request->all()

http://laravel.com/docs/5.0/requests#retrieving-inputhttp://laravel.com/docs/5.1/requests#retrieving-input

http://laravel.com/docs/5.0/requests#retrieving-input http://laravel.com/docs/5.1/requests#retrieving-input

EDIT: This post gives some more in-depth information: https://stackoverflow.com/a/29961400/2433843

编辑:这篇文章提供了一些更深入的信息:https: //stackoverflow.com/a/29961400/2433843

EDIT3: I think it would be great if someone could explain WHY exactly you can't inject Facades into your Controllers. I understand DI and Facades are two different things entirely, and L5+ is pushing the developers towards DI. I just don't exactly understand why injecting a facade wouldn't work, since it points towards another class, and it works when you do not inject it. Not to forget Facades and Aliases are two seperate things too. I hope someone can elaborate on this.

EDIT3:我认为如果有人能解释为什么你不能将 Facades 注入到你的控制器中,那就太好了。我知道 DI 和 Facades 是两个完全不同的东西,L5+ 正在推动开发人员转向 DI。我只是不完全理解为什么注入外观不起作用,因为它指向另一个类,并且当您不注入它时它会起作用。不要忘记 Facades 和 Aliases 也是两个独立的东西。我希望有人可以详细说明这一点。

回答by Vinod Tigadi

One more important thing about using Requestor Inputto access the User Input is the version of Laravel that you are using.

使用RequestInput访问用户输入的另一件重要事情是您使用的 Laravel 版本。

In the Laravel 4.2 and prior, you could have access the Input::all(), Input::get()but from Laravel 5 onwards, it's been suggested to use the Input via Requestfacade

在 Laravel 4.2 及更早版本中,您可以访问Input::all()Input::get()但从 Laravel 5 开始,建议使用 Input via RequestFacade

Ref: https://laravel.com/docs/5.2/requests

参考:https: //laravel.com/docs/5.2/requests

In case if you want to make use of Inputin Laravel 5.0 and onwards, then you need to add this facade in the config/app.phpfile under the aliasessection as 'Input' => Illuminate\Support\Facades\Input::class

如果你想在 Laravel 5.0 及以后的版本中使用Input,那么你需要在config/app.php文件的aliases部分下添加这个 Facade为'Input' => Illuminate\Support\Facades\Input::班级

Once you add the facade under alias, you should start using the 'Input::all()'

在别名下添加外观后,您应该开始使用 'Input::all()'

Hope this helps some others, who are having the confusion as whether to use 'Input' or 'Request' for Laravel 5.0 onwards.

希望这可以帮助其他一些人,他们对 Laravel 5.0 以后是否使用“输入”或“请求”感到困惑。