Home >PHP Framework >Laravel >Summarize the usage of $request to obtain request information in laravel
The following tutorial column from laravel will summarize the usage of $request in laravel to obtain request information. I hope it will be helpful to friends in need!
Summary of some methods available for $request in laravel
1. Obtaining request methods
$method = $request->method();
2. Detecting requests Method
$res = $request->isMethod('post')
3, get the requested path
$path = $request->path()
4, get the complete url
$url = $request->url();
5, get the requested ip
$ip = $request->ip()
6, get Port
$port = $request->getPort();
7, get parameters
$name = $request->input('name')
8, set default value
$res = $request->input('name','10')
9, detect request parameters
$res = $request->has('name')
10, get all request parameters
$res = $request->all()
11, extract some parameters
$res = $request->only(['username','password'])
12, remove unnecessary parameters
$res = $request->except(['username','password'])
13, obtain request header information
$res = $request->header('Connection')
14, detect files Whether there is upload
$res = $request->hasFile('cover')
15, extract the uploaded file
$res = $request->file('file');
16, get cookie
$cookies = $request->cookie();
17, add cookie value
$response->withCookie(cookie('cookie','learn-laravel',3)); //第一个参数是cookie名,第二个参数是cookie值,第三个参数是有效期(分钟). $response->withCookie(cookie()->forever('cookie-name','cookie-value'));
The above is the detailed content of Summarize the usage of $request to obtain request information in laravel. For more information, please follow other related articles on the PHP Chinese website!