Try   HackMD

學 Laravel 一定要知道的 Restful Resource



Hashman Lin

coosos810609@gmail.com

KKBOX Engineer


About Me

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

  • 林良俞 a.k.a Hash
  • 現: KKBOX 工程師
  • 環球誠信 F2E 工程師
  • 竣盟科技產品工程師
  • MOPCON 2015.2016 場務組組長

工商服務時間

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →


  • KKBOX
    • 前端工程師(高雄、台北)
    • 後端工程師(高雄、台北)
    • Billing(高雄
  • KKSteam

更多相關職缺可以參考 104


工商服務時間

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

MOPCON 2016

10/29 - 10/30

高雄國際會議中心 3.4F


Restful

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →


介紹之前

前幾天剛好看到


PHP 也有 Day #26 - REST API 與前端整合之踩雷心得

By Ricky

https://www.youtube.com/watch?v=CRaiwg-SLh0&feature=youtu.be


Restful

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →


什麼是 RestFul

  • 每個 resource 都有獨立的 URI
  • 全部都透過一致的介面進行轉換
  • Well defind operations

Well-defind operations

  • GET
  • POST
  • DELETE
  • PUT
  • PATCH

RestFul 的優點

  • 支援 Cache 改善反應時間與 Server 負載
  • 可以支援平行擴充
  • 瀏覽器可以搞定一切
  • 只需要 HTTP 不需要依靠其他的機制

常見架構


All-In-One

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →


  • 成本低
  • 管理方便簡單
  • 架構單純(可以透過 Auth 去進行權限管理)

Laravel Auth

<?php

namespace App\Http\Controllers;

use Auth;
use Illuminate\Routing\Controller;

class AuthController extends Controller
{
    /**
     * Handle an authentication attempt.
     *
     * @return Response
     */
    public function authenticate()
    {
        if (Auth::attempt(['email' => $email, 'password' => $password])) {
            // Authentication passed...
            return redirect()->intended('dashboard');
        }
    }
}

檢查有沒有 login

if (Auth::check()) {
    // The user is logged in...
}

Logout

Auth::logout();

前後分離

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →


Many to Many

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →


Many to Many 好處

  • Scale out
  • Load balance
  • 前後端分離,架構清晰

Scale out vs. Scale up


出處:http://www.ems5.com/view.php?id=308


前後端分離該怎麼實現驗證機制?


JWT

Imgur
https://jwt.io/


JWT

  • JWT - JSON Web Tokens
  • 由 Header, Payload, Signature 三者共同組成
  • 詳細可以參考JWT Introduction

JWT Debugger

Imgur
https://jwt.io/


當然 Laravel 也有


https://github.com/tymondesigns/jwt-auth


Laravel 實踐前後端配合


我想這個應該蠻常見

Route::get('user/all_user', 'UserController@getAllUser');
Route::get('user/{id}/get', 'UserController@getUser');
Route::post('user/create', 'UserController@createUser');
Route::put('user/{id}/update', 'UserController@updateUser');
Route::delete('user/{id}/delete', 'UserController@deleteUser');

缺點

  • Route 肥大不易維護
  • Controller 不易維護

會不會太~~~麻煩了


使用 Laravel RestFul resource

Route::resouce('user', 'UserController');

一行搞定


Laravel 幫你定義好了

​Route::resource(‘user’, ‘UserController’);
​/user => index(GET)
​/user/create => create(GET)
​/user => store(POST)
​/user/{id} => show(GET)
​/usr/{id}/edit => edit(GET)
​/user/{id} => update(PUT/PATCH)
​/user/{id} => destroy(DELETE)

  • index: 通常用於 query 全部資料
  • show: 用於取得單筆資料
  • store: 建立新資料
  • update: 更新單筆資料
  • destroy: 刪除單筆資料

Laravel 5.1 RestFul Resource Controller


Laravel RestFul Controller 優點

  • Route 簡潔又乾淨
  • Controller 好追蹤
  • 前端串接容易


出處:https://www.idisciple.org/post/claim-your-victory


但一定會不夠用

Route::get('user/somethingyouneed', 'UserController@someThingYouNeed');
Route::resource('user', 'UserController');

就慢慢的加上去嚕~~


Q&A


還有個小 Demo


Demo

  • New laravel project
  • migrate with seeder using sqlite
  • Restful Controller
  • Refactor Controller

Github - Laravel-Restful-demo