laravel 访问控制器中的当前请求
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31900229/
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
Accessing the current request in controller
提问by PHPst
In other MVC frameworks, accessing to the current request object is as simple as $this->request
. However in the Laravel, I generally see that Request $request
is generally injected to each action (public function edit($id, Request $request)
). It seems like a boilerplate. Is there any better way to access the request? (I now that I can use inheritance to use $this->request
, I am looking for the Laravel way to do that.)
在其他 MVC 框架中,访问当前请求对象就像$this->request
. 但是在 Laravel 中,我一般看到Request $request
一般是注入到每个动作中(public function edit($id, Request $request)
)。这似乎是一个样板。有没有更好的方法来访问请求?(我现在可以使用继承来使用$this->request
,我正在寻找 Laravel 的方式来做到这一点。)
update:
更新:
I found out using app('request')
I can access to the current request. However, I am not sure of its potential pros and cons.
我发现使用app('request')
我可以访问当前请求。但是,我不确定它的潜在优点和缺点。
回答by miken32
In Laravel 5, you can use the request()
helper:
在 Laravel 5 中,您可以使用request()
帮助程序:
// to get the current request object
$request = request();
// or to just get a value from the request
$value = request("field", "default");