Laravel 的 Illuminate\Http\Request 类拓展自 Symfony 的 HttpFoundation\Request 类.
tip: Symfony 的 HttpFoundation 组件几乎应用于所有每一个php框架, 它设置了一组抽象的变量集合, 用来表示 HTTP 请求,响应,header,cookie等, 很受欢迎.
Request 对象,用于表示有关用户的 HTTP 请求的 所有你想知道的有用的信息.
原生 php 中, 你需要访问 $_SERVIER,$_GET,$_POST 等全局变量才能获得需要的请求信息. 有时更复杂,需要对全局变量进行处理.
Symfony’s Request 对象, 整合了单个HTTP请求所有用于信息到一个对象中, 并提供便利的方法来获取这些信息. laravel 的 Illuminate Request 对象添加了更多便捷方法来获取有关其所代表的请求的信息。
捕获请求: larave 中你几乎不用手动捕获请求, 如果你需要的化使用
capture()方法:
$request = Illuminate\Http\Request::capture();
获取 request 对象
- 您可以在容器解析的任何构造函数或方法中键入该类的提示(见 11 章)。 即 任何一个控制器方法或者服务提供者方法.
...
use Illuminate\Http\Request;
class PeopleController extends Controller
{
public function index(Request $request)
{
$allInput = $request->all();
}
request()全局方法
$request = request();
$allInput = $request->all();
// or
$allInput = request()->all();
app()全局方法 获取请求对象实例.
$request = app(Illuminate\Http\Request::class);
$request = app('request');
获取请求的基本信息
基本用户输入
- all()
Returns an array of all user-provided input.
- input(fieldName)
Returns the value of a single user-provided input field.
- only(fieldName|[array,of,field,names])
Returns an array of all user-provided input for the specified field name(s).
- except(fieldName|[array,of,field,names])
Returns an array of all user-provided input except for the specified field name(s).
- exists(fieldName)
Returns a Boolean indicating whether the field exists in the input. has() is an alias.
- filled(fieldName)
Returns a Boolean indicating whether the field exists in the input and is not empty (that is, has a value).
- json()
Returns a ParameterBag if the page had JSON sent to it.
- json(keyName)
Returns the value of the given key from the JSON sent to the page.
有时 laravel 会返回 ParameterBag 对象. 它有点像关联数组.
$bag->get('name'): 获取 key 的值
$bag->has('name'): 获取 ‘name’ 是否存在
$bag->all(): 返回 一个数组 类似于 [key=>value]
$bag->keys(): 返回 一个数组 只有 key
用户和请求状态
method()
返回连接该路由的方法 (GET, POST, PATCH 等)
path()
返回路径 (不带域名) , eg: http://www.myapp.com/abc/def -> abc/def
url()
返回url (带域名), eg: http://www.myapp.com/abc/def -> http://www.myapp.com/abc/def
is()
返回当前页是否模糊匹配提供的字符串, eg: /a/b/c 匹配
$request->is('*b*'); 使用正则的话,参数可以参考Str::is()
ip()
返回用户IP
header()
不传参数 返回
header的数组; 传参 返回header数组中的这个key对应的值
server()
返回存储在
$_SERVER全局变量中变量数组, eg: REMTE_ADDR ; 如果你传入了变量名, 将只返回该变量对应的值.
secure()
返回是否 HTTPS
pjax()
返回 是否使用 Pjax 加载页面请求
wentsJson()
返回一个布尔值,该布尔值指示此请求的Accept标头中是否具有任何/ json内容类型。
isJson()
返回一个布尔值,指示此页面请求的Content-Type标头中是否具有任何/ json内容类型。
accepts()
返回一个布尔值,指示此页面请求是否接受给定的内容类型。
文件
file()
不传参 返回所有已经上传的文件; 传入 key (文件上传字段名称), 仅仅返回该字段的上传的文件.
allFiles()
返回所有已经上传的文件, 等价于
file(), 但是名称更加清晰.
hasFile()
返回一个布尔值,指示是否在指定的密钥(文件上传字段名称)上载了文件
上传的每个文件都是Symfony\Component\HttpFoundation\File\UploadedFile的实例,该实例提供了一套用于验证,处理和存储上传文件的工具.(见 14 章)
持久化 (Persistence)
请求还可以提供用于与会话(session)进行交互的功能。大多数会话功能都位于其他位置,但是这里有一些与当前页面请求特别相关的方法:
flash()
把当前请求的用户输入闪存到之后要被回收的
session中, 即, 保存到会话中, 下一个请求会就会消失.
flashOnly()
仅仅 闪存 提供 key 值的用户输入.
flashExcept()
除了 提供 key 值的之外, 都闪存.
old()
不传参数, 返回所有之前闪存的用户输入; 传参, 仅仅返回该键值的闪存的用户输入.
flush()
擦除之前所有闪存的用户输入
cookie()
不传参数, 从请求中获取
cookie, 传参, 获取特定cookie
hasCookie()
检查是否存在特定的
cookie
flash*() 和 old() 方法通常用于 在表单验证失败拒绝提交之后, 记录用户输入并以后恢复它。
