Home > PHP Framework > Laravel > Summarize the usage of $request to obtain request information in laravel

Summarize the usage of $request to obtain request information in laravel

藏色散人
Release: 2021-04-22 09:04:51
forward
3065 people have browsed it

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!

Summarize the usage of $request to obtain request information in laravel

Summary of some methods available for $request in laravel
1. Obtaining request methods

$method = $request->method();
Copy after login

2. Detecting requests Method

$res = $request->isMethod('post')
Copy after login

3, get the requested path

$path = $request->path()
Copy after login

4, get the complete url

$url = $request->url();
Copy after login

5, get the requested ip

$ip = $request->ip()
Copy after login

6, get Port

$port = $request->getPort();
Copy after login

7, get parameters

$name = $request->input('name')
Copy after login

8, set default value

$res = $request->input('name','10')
Copy after login

9, detect request parameters

$res = $request->has('name')
Copy after login

10, get all request parameters

$res = $request->all()
Copy after login

11, extract some parameters

$res = $request->only(['username','password'])
Copy after login

12, remove unnecessary parameters

$res = $request->except(['username','password'])
Copy after login

13, obtain request header information

$res = $request->header('Connection')
Copy after login

14, detect files Whether there is upload

$res = $request->hasFile('cover')
Copy after login

15, extract the uploaded file

$res = $request->file('file');
Copy after login

16, get cookie

$cookies = $request->cookie();
Copy after login

17, add cookie value

$response->withCookie(cookie('cookie','learn-laravel',3));  
//第一个参数是cookie名,第二个参数是cookie值,第三个参数是有效期(分钟).
$response->withCookie(cookie()->forever('cookie-name','cookie-value'));
Copy after login




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!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template