laravel中$request可用的一些方法小結
1,請求方法的獲取
$method = $request->method();
2,檢測請求方法
$res = $request->isMethod('post');
3,獲取請求的路徑
$path = $request->path();
4,獲取完整的url
$url = $request->url();
5,獲取請求的ip
$ip = $request->ip();
6,獲取端口
$port = $request->getPort();
7,參數的獲取
$name = $request->input('name');
8,設置默認值
$res = $request->input('name','10');
9,檢測請求參數
$res = $request->has('name');
10,獲取所有請求參數
$res = $request->all();
11,提取部分參數
$res = $request->only(['username','password']);
12,剔除不需要的參數
$res = $request->except(['username','password']);
13,獲取請求頭信息
$res = $request->header('Connection');
14,檢測文件是否有上傳
$res = $request->hasFile('cover');
15,提取上傳的文件
$res = $request->file('file');
16,獲取cookie
$cookies = $request->cookie();
17,新增cookie值
$response->withCookie(cookie('cookie','learn-laravel',3)); //第一個參數是cookie名,第二個參數是cookie值,第三個參數是有效期(分鍾) $response->withCookie(cookie()->forever('cookie-name','cookie-value')); //如果想要cookie長期有效使用此方法
未完待續......