在完成 Model 與 Migration 的設置後,我們將會定義 API routes,在講到 api routes 之前,先來介紹一下 Laravel 基本的 route 概念。
各自涵義請看 Restful API 介紹
Route::get
Route::post
Route::delete
Route::patch
Route::put
# 於 routes\api.php 最尾端加上
Route::get('/test', function () {
return "hi";
});
Route::get('/test/{id}', function ($id) {
return $id;
});
# 在專案根目錄下指令
php artisan serve
# 打開網頁瀏覽器即可測試
http://127.0.0.1:8000/api/test
#回應應該是 hi
http://127.0.0.1:8000/api/test/1
#回應應該是 1
# 假設你是 xampp,網頁根目錄是 C:\xampp\htdocs
# 專案位置在 C:\xampp\htdocs\laravel-learn
# 那網址就會是
http://localhost/laravel-learn/public/api/test
#回應應該是 hi
http://localhost/laravel-learn/public/api/test/1
#回應應該是 1
# 最上面引入 Controller class
use App\Http\Controllers\UserController;
# 底部加入
Route::post('/users/register', [UserController::class, "register"]);
Route::post('/users/login', [UserController::class, "login"]);
Route::prefix('users')->group(function () {
Route::post('/register', [UserController::class, "register"]);
Route::post('/login', [UserController::class, "login"]);
});
# 在專案根目錄下指令
php artisan serve
# 打開網頁瀏覽器即可測試
# 註冊
http://127.0.0.1:8000/api/users/register
# 假設你是 xampp,網頁根目錄是 C:\xampp\htdocs
# 專案位置在 C:\xampp\htdocs\laravel-learn
# 那網址就會是
# 註冊
http://localhost/laravel-learn/public/api/users/register
# 登入
http://localhost/laravel-learn/public/api/users/login
# 回傳和資料與上同樣
# 註冊
# 使用 POST
# body
# 在 postman 使用 form-data or raw
{
"name":"test",
"email":"[email protected]",
"password":"testtest",
"password_confirmation":"testtest"
}
# 回應應該類似以下
{
"statusCode": 200,
"message": "創建成功",
"userData": {
"name": "test123",
"email": "[email protected]",
"updated_at": "2023-07-18T04:06:50.000000Z",
"created_at": "2023-07-18T04:06:50.000000Z",
"id": 4
}
}
# -----------------------------------
# 不符合驗證登入資料
{
"name":"test123",
"email":"[email protected]",
"password":"test",
"password_confirmation":"testtest"
}
# 回應應該類似以下
{
"statusCode": 400,
"message": "註冊失敗",
"error": {
"email": [
"信箱已被註冊"
],
"password": [
"password 最短6碼",
"與密碼驗證不符"
]
}
}
# 登入
# 使用 POST
# body
# 在 postman 使用 raw
http://127.0.0.1:8000/api/users/login
# 符合驗證登入資料
{
"email":"[email protected]",
"password":"testtest"
}
# 回應應該類似以下
{
"statusCode": 200,
"message": "登入成功",
"name": "test"
}
# -----------------------------------
# 不符合驗證登入資料
{
"email":"test",
"password":"test"
}
# 回應應該類似以下
{
"statusCode": 400,
"message": "登入失敗",
"errors": {
"email": [
"格式必須符合 email 格式"
],
"password": [
"password 最短6碼"
]
}
}