laravel8創建基礎api接口


laravel8創建基礎api接口

一、建立實體類

php artisan make:model api/User

二、建立User控制器

php artisan make:controller UserController --api

三、建立api控制類

php artisan make:controller ApiController

四、User控制器繼承api控制類

五、分頁

// 獲取全部可顯示字段
$data = User::get();
// 獲取指定字段 
$data = User::select('id','name','email')->get();
// 獲取指定字段並分頁
$data = User::select('id','name','email')->paginate(10);

六、404錯誤頁面

  1. 在 resources/views 目錄下創建 errors 文件夾
  2. 在 resources/views/errors 目錄下創建 404.blade.php 文件,並編寫返回的內容

七、展示數據

請求方式:GET

public function show($id)
    {
        // 獲取數據
        // 數據存在 返回成功信息
        // 數據不存在
        // 參數超出范圍導致數據不存在
        // 參數類型錯誤導致數據不存在

        $data = User::select('id', 'name', 'email')->find($id);

        if (isset($data)) {
            return $this->create('數據獲取成功', 200, $data);
        } else {
            if (is_numeric($id)) {
                return $this->create('用戶不存在', 204, []);
            } else {
                return $this->create('id 參數錯誤', 400, []);
            }
        }
    }

八、新增數據

請求方式:POST

public function store(Request $request)
    {
        // 接收數據
        $data = $request->all();
        // 數據驗證
        $validator = Validator::make($data, [
            'name' => 'required|min:2|max:50',
            'email' => 'required|min:2|max:50|email',
            'password' => 'required|min:6|max:50',
        ]);
        if ($validator->fails()) {
            return $this->create($validator->errors(), 400);
        } else {
            $newUser = User::create([
                'name' => $request->name,
                'email' => $request->email,
                'password' => md5($request->password),
            ]);
            $newUser = User::select('id', 'name', 'email')->find($newUser['id']);
            return $this->create('用戶創建成功', 200, $newUser);
        }
    }

九、刪除數據

請求方式:DELETE

public function destroy($id)
    {
        // 獲取數據
        // 數據存在,刪除
        // 數據不存在
        // 參數超出范圍導致數據不存在
        // 參數類型錯誤導致數據不存在
        $data = User::find($id);
        if (isset($data)) {
            if ($data->delete()) {
                return $this->create('用戶刪除成功', 200);
            }
        } else {
            if (is_numeric($id)) {
                return $this->create('用戶不存在,無法刪除', 204, []);
            } else {
                return $this->create('id 參數錯誤', 400, []);
            }
        }
    }

十、修改數據

請求方式:PUT

public function update(Request $request, $id)
    {
        // 接收數據
        // 驗證數據
        // 查找用戶
        // 如果用戶不存在
        // 更新並返回信息
        $data = $request->all();
        $validator = Validator::make($data, [
            'name' => 'required|min:2|max:50',
            'password' => 'required|min:6|max:50',
        ]);
        if ($validator->fails()) {
            return $this->create($validator->errors(), 400);
        }
        $user = User::find($id);
        if (isset($user)) {
            if ($user->update([
                'name' => $request->name,
                'password' => md5($request->password),
            ])) {
                return $this->create('用戶更新成功', 200);
            }
        } else {
            if (is_numeric($id)) {
                return $this->create('用戶不存在,無法更新', 204, []);
            } else {
                return $this->create('id 參數錯誤', 400, []);
            }
        }
    }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM