控制器外部的 Laravel 访问请求对象

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

Laravel access request object outside controller

phplaravellaravel-5.2

提问by xelber

According to the Laravel documentation Request is acquired via dependency injection. For a controller this is fine, but how do we access Request object outside a controller, for example in a view

根据 Laravel 文档,请求是通过依赖注入获取的。对于控制器来说这很好,但是我们如何访问控制器外部的请求对象,例如在视图中

回答by Bishnu Bhusal

There is request helper in laravel. You can use Request Object anywhere. For example

Laravel 中有请求助手。您可以在任何地方使用请求对象。例如

request()->field_name 

Here's laravel documentation link for request helper https://laravel.com/docs/5.2/helpers#method-request

这是请求助手的 laravel 文档链接 https://laravel.com/docs/5.2/helpers#method-request