-
-
- 1. 擷取位址
-
- 1) 輸出目前URL位址
- 2) 擷取URL的路由
- 3) 擷取 URL位址(除參數外)
- 4) 判斷目前路由是否是 request
-
- 2. 擷取請求類型
-
- 1) 判斷目前的請求類型
- 2) 判斷目前的請求是否是 特定的請求(如:是否為get請求)
- 3) 使用
- 4) 請求資料(後期結合資料庫使用)
- 以下為上述完整的代碼:
-
- 3. 判斷請求類型的使用
- 4. 閃存
-
- 1) 将表單送出過來的所有資料 寫入閃存
- 2) 把誰寫入閃存
- 3) 除了誰,其他的都寫入閃存
- 4)把目前所有的資料都寫入閃存
-
- 5. 圖檔上傳
-
- 0) 擷取圖檔的上傳路徑
- 1) 擷取上傳的檔案
- 2) 判斷檔案是否上傳
-
- 6、Cookie
-
- 1)Cookie 的使用場景
- 2)注意:laravel 架構把所有的 Cookie 進行了加密,是以
- 3)檢視 cookie
- 4) 設定cookie
-
- 1. 擷取位址
-
準備工作:将之前 web.php中的所有路由都注釋掉(為了避免有沖突,本例子是為了測試使用)。
- 在 web.php 中定義路由:
Route::get('request', "[email protected]");
- 在 D:\phpStudy\PHPTutorial\WWW\laravel\app\Http\Controllers 下 建立控制器 RequestController.php(可通過 artisan建立)
- 在 控制器 RequestController.php 中寫方法 index
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
// laravel的請求
class RequestController extends Controller
{
public function index(Request $request)
{
dd($request);
}
}
注:以下在
RequestController.php
中的
index
方法 中執行,且位址欄通路
www.la.com/request?name=zhangsan
1. 擷取位址
1) 輸出目前URL位址
2) 擷取URL的路由
3) 擷取 URL位址(除參數外)
echo $request->url(); // 輸出結果:
http://www.la.com/request
4) 判斷目前路由是否是 request
2. 擷取請求類型
1) 判斷目前的請求類型
echo $request->method();
2) 判斷目前的請求是否是 特定的請求(如:是否為get請求)
傳回值:true|false
var_dump($request->isMethod('POST'));
3) 使用
public function add(Request $request)
{
// 判斷目前是否為 get請求
if ($request->isMethod('GET')) {
// 加載添加頁面
return view('add');
} else {
// 資料庫的入庫操作
echo "插入資料庫";
}
}
4) 請求資料(後期結合資料庫使用)
① 擷取所有的請求資料
$data = $request->all();
② 擷取特定字段
echo $request->input('name');
③ 設定預設值(如果有值,顯示對應的值;沒有則使用預設值)
echo $request->input('age', '18');
④ 判斷資料是否存在
傳回值: true | false
var_dump($request->has('username'));
⑤ 擷取部分輸入資料
// 兩種方式都可以擷取到
$data1 = $request->only('name','pass');
$data1 = $request->only(['name', 'pass']);
var_dump($data1);
⑥ 擷取除指定資料以外的資料
$data2 = $request->except('_token', 'name');
$data2 = $request->except(['_token', 'name']);
var_dump($data2);
以下為上述完整的代碼:
- UserController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
class UserController extends Controller
{
// 使用者添加
public function add(Request $request)
{
// 判斷目前是否為 get請求
if ($request->isMethod('GET')) {
// 加載添加頁面
return view('add');
} else {
// 存入資料庫
echo "存入資料庫";
// 1. 擷取所有的請求資料
$data = $request->all();
dd($data);
// 2. 擷取特定字段
echo $request->input('name');
// 3. 設定預設值
echo $request->input('sex', '男');
// 4. 判斷送出的資料是否存在(傳回值: true | false)
var_dump($request->has('sex'));
// 5. 擷取部分輸入資料
$data1 = $request->only('name', 'pass');
$data1 = $request->only(['name', 'pass']);
var_dump($data1);
// 6. 擷取除指定資料外的資料
$data2 = $request->except('_token', 'name');
$data2 = $request->excepte(['_token', 'name']);
var_dump($data2);
}
}
}
3. 判斷請求類型的使用
若該請求為 get 請求,則 加載使用者添加頁面 ,否則 将資料存入資料庫
- route / web.php
Route::match(['get', 'post'], 'admin/user/add', "[email protected]");
- app / controllers / UserController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Request;
class UserController extends Controller
{
// 使用者添加
public function add(Request $request)
{
// 判斷目前是否為 get請求
if ($request->isMethod('GET')) {
// 加載添加頁面
return view('add');
} else {
// 存入資料庫
echo "存入資料庫";
}
}
}
- resource / views / add.blade.php
<!doctype html>
<html hljs-string">"en">
<head>
<meta charset="UTF-8">
<title>使用者新增</title>
</head>
<body>
<form action="/admin/user/add" method="post">
{{csrf_field()}}
<p>
User:
<input type="text" name="name">
</p>
<p>
Pass:
<input type="password" name="pass">
</p>
<p>
<input type="submit" value="送出">
</p>
</form>
</body>
</html>
4. 閃存
作用:會将合法的字段值存入閃存,不合法的清空(若:在注冊資訊的時候,使用者名,郵箱都正确,但是電話号碼出錯,此時會将表單送出過來的全部資料存入閃存,為了提高使用者的體驗度)
1) 将表單送出過來的所有資料 寫入閃存
$request->flash();
2) 把誰寫入閃存
$request->flashOnly('name', 'tel', 'email');
3) 除了誰,其他的都寫入閃存
// 除了 tel,其他都寫入閃存(密碼無法寫入閃存)
$request->flashExcept('tel');
4)把目前所有的資料都寫入閃存
return back()->with('errors', "使用者名長度不滿足")->withInput();
執行個體:注冊為例
- web.php
// 注冊頁面
Route::get('reg', "[email protected]");
// 注冊操作
Route::post('insert', "[email protected]");
- resources / views / reg.blade.php
<!doctype html>
<html hljs-string">"en">
<head>
<meta charset="UTF-8">
<title>注冊</title>
</head>
<body>
<?php
// $errors 為報錯資訊
if (!is_object($errors)) {
echo "<h1>$errors</h1>";
}
?>
<form action="/insert" method="post">
<p>
{{csrf_field()}}
User:
<input type="text" name="name" value="{{old('name')}}">
</p>
<p>
Pass:
<input type="password" name="pass">
</p>
<p>
Repass:
<input type="password" name="repass">
</p>
<p>
Tel:
<input type="text" name="tel" value="{{old('tel')}}">
</p>
<p>
Email:
<input type="text" name="email" value="{{old('email')}}">
</p>
<p>
<input type="submit" value="送出">
</p>
</form>
</body>
</html>
- app/Http/LoginController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
class LoginController extends Controller
{
// 注冊頁面
public functin index()
{
return view('reg');
}
// 注冊功能
public function insert(Request $request)
{
$name = $request->input('name');
// 寫入閃存
// 除了 tel 其他都存入 閃存
$request->flashExcept('tel');
// 判斷使用者名長度
if (strlen($name) >= && strlen($name) <= ) {
} else {
// 回退到上一個頁面
return back()->with('errors', "使用者名長度不滿足")->withInput();
}
}
}
- 通路 www.la.com/reg
- 運作結果:

5. 圖檔上傳
0) 擷取圖檔的上傳路徑
- 方法一:
var_dump($_FILES);
運作結果:
array() {
["img"]=>
array() {
["name"]=>
string() "注冊界面-Laravel閃存.png"
["type"]=>
string() "image/png"
["tmp_name"]=>
string() "C:\Windows\php542F.tmp"
["error"]=>
int()
["size"]=>
int()
}
}
* 方法二:
dd($requeset->all());
運作結果:
array: [▼
"_token" => "iDFA4HHtnuTL2AvXcf69fUgHXXF1KhGUwYH6MRgU"
"img" => UploadedFile {#365 ▼
-test: false
-originalName: "注冊界面-Laravel閃存.png"
-mimeType: "image/png"
-size:
-error:
path: "C:\Windows"
filename: "phpC183.tmp"
basename: "phpC183.tmp"
pathname: "C:\Windows\phpC183.tmp"
extension: "tmp"
realPath: "C:\Windows\phpC183.tmp"
aTime: -- ::
mTime: -- ::
cTime: -- ::
inode:
size:
perms:
owner:
group:
type: "file"
writable: true
readable: true
executable: false
file: true
dir: false
link: false
linkTarget: "C:\Windows\phpC183.tmp"
}
]
1) 擷取上傳的檔案
// 方法一:
$img = $request->file('img'); // img為表單中 的name
// 方法二:
$img2 = $request->img;
dd($img);
列印結果如下:
2) 判斷檔案是否上傳
傳回值:true | false
var_dump($request->hasFile('img'));
執行個體:
- web.php
// 檔案上傳
Route::get('photo', "[email protected]");
// 處理檔案上傳
Route::post('upload', "[email protected]");
- resources / views / photo.blade.php
<!doctype html>
<html hljs-string">"en">
<head>
<meta charset="UTF-8">
<title>圖檔上傳</title>
</head>
<body>
<form action="/upload" method="post" enctype="multipart/form-data">
<p>
{{csrf_field()}}
Photo:
<input type="file" name="img">
</p>
<p>
<input type="submit" value="送出">
</p>
</form>
</body>
</html>
- app / Http / Controllers / UserController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use app\Http\Requests;
class UserController extends Controller
{
// 圖檔上傳頁面
public function photo()
{
return view('photo');
}
// 檔案上傳處理
public function upload(Request $request)
{
// 如何實作檔案上傳
// 1.1 判斷是否上傳檔案
// 1.2 把檔案移動到指定目錄
// 擷取上傳的檔案
// 方法一:
$img = $request->file('img');
// 方法二:
$img2 = $request->img;
// 1. 判斷檔案是否上傳(傳回值: true | false)
if ($request->hasFile('img')) {
// 擷取檔案的字尾名
$ext = $request->file('img')->getClientOriginalExtension();
// 建立新的檔案名(時間戳 + 随機數 + 字尾名)
$newFile = time().rand().".".$ext;
// 2. 上傳檔案操作(把上傳的檔案移到指定目錄)
$request->file('img')->move('./Uploads');
} else {
// 回到上一個頁面
return back();
}
}
}
- 在 public 下 建立一個 Uploads 檔案夾,用來存儲上傳的圖檔
- 通路 www.la.com/photo
bug:該代碼實作并未限制 上傳檔案的類型 ,是以圖檔,檔案等都可以上傳
(一般很少用該種上傳方式,一般都用 無重新整理上傳檔案,因為界面友好,使用者體驗度高)
6、Cookie
1)Cookie 的使用場景
- 登入
- 如果未勾選自動登入,預設采用 session(session關閉浏覽器會失效)
- 如果勾選了自動登入,預設采用 cookie
- 網站浏覽記錄
2)注意:laravel 架構把所有的 Cookie 進行了加密,是以
$_COOKIE['name'] != $request->cookie('name');
3)檢視 cookie
① 檢視所有 cookie
dd($request->cookie());
// 運作結果:
array: [▼
"XSRF-TOKEN" => "iDFA4HHtnuTL2AvXcf69fUgHXXF1KhGUwYH6MRgU"
"laravel_session" => "8a79635d0d58fae23d7ca2abd939661943bdc192"
]
② 檢視 指定 cookie
// 方法一:
dd($request->cookie('laravel_session'));
echo $request->cookie('laravel_session');
// 方法二:
echo \Cookie::get('laravel_session');
4) 設定cookie
// 方法一:
\cookie::queue('boy', 'zhangsan', );
// 方法二:
$cookie = cookie('name', 'xiaoming', );
return response('Hello Laravel')->cookie($cookie);
完整代碼執行個體:
- web.php
// 檢視 cookie
Route::get('cookie', "[email protected]");
- app / Http / Controllers / UserController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
class UserController extends Controller
{
// 建立 cookie 方法
public function cookie(Request $request)
{
// 檢視所有的cookie
dd($request->cookie());
// 檢視指定的 cookie
// 方法一:
dd($request->cookie('laravel_session'));
echo $request->cookie('laravel_session');
// 方法二:
echo \Cookie::get('laravel_session');
// 設定cookie
// 方法一:
\Cookie::queue('boy', 'zhangsan', );
// 方法二:
$cookie = cookie('name', 'xiaoming', );
return response('Hello Laravel')->cookie($cookie);
}
}
運作結果: