# 學 Laravel 一定要知道的 Restful Resource <br><br> #### Hashman Lin #### coosos810609@gmail.com #### KKBOX Engineer --- # About Me <img src="https://dl.dropboxusercontent.com/u/33150136/me.jpg" width="250"><br> - 林良俞 a.k.a Hash - 現: KKBOX 工程師 - 環球誠信 F2E 工程師 - 竣盟科技產品工程師 - MOPCON 2015.2016 場務組組長 --- # 工商服務時間 ![](https://www.kkbox.com/images/fb/kkbox_logo_1200.jpg) ---- - KKBOX - 前端工程師(高雄、台北) - 後端工程師(高雄、台北) - Billing([高雄](http://www.104.com.tw/job/?jobno=46m2r&jobsource=)) - KKSteam - 前端工程師([高雄](http://www.104.com.tw/job/?jobno=50cpg&jobsource=)) - 後端工程師([高雄](http://www.104.com.tw/job/?jobno=50cq6&jobsource=)) <br> 更多相關[職缺](http://www.104.com.tw/jobbank/custjob/index.php?r=cust&j=624a43255e46406a30683b1d1d1d1d5f2443a363189j50&page=1#info06)可以參考 104 --- # 工商服務時間 ![](https://scontent-tpe1-1.xx.fbcdn.net/v/t1.0-9/13240013_876473352478382_8478334807582206913_n.png?oh=0f6b637699228ab22cc7fb96d0ffe18c&oe=5816DFBA) #### [MOPCON 2016](http://mopcon.org/2016/) #### 10/29 - 10/30 #### 高雄國際會議中心 3.4F --- # Restful <img src="https://www.drupal.org/files/styles/grid-3/public/project-images/rain-drop-hi.png?itok=qtY1ywQX" width="250"><br> --- # 介紹之前... # 前幾天剛好看到 ---- # PHP 也有 Day #26 - REST API 與前端整合之踩雷心得 ### By Ricky https://www.youtube.com/watch?v=CRaiwg-SLh0&feature=youtu.be --- # Restful <img src="https://www.drupal.org/files/styles/grid-3/public/project-images/rain-drop-hi.png?itok=qtY1ywQX" width="250"><br> ---- # 什麼是 RestFul - 每個 resource 都有獨立的 URI - 全部都透過一致的介面進行轉換 - Well defind operations ---- # Well-defind operations - GET - POST - DELETE - PUT - PATCH ---- # RestFul 的優點 - 支援 Cache 改善反應時間與 Server 負載 - 可以支援平行擴充 - 瀏覽器可以搞定一切 - 只需要 HTTP 不需要依靠其他的機制 --- # 常見架構 --- # All-In-One ![Imgur](http://i.imgur.com/ggiWgjQ.jpg) ---- - 成本低 - 管理方便簡單 - 架構單純(可以透過 Auth 去進行權限管理) ---- # Laravel Auth ```php <?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'); } } } ``` - [Laravel 5.1 Authentication](https://laravel.com/docs/5.1/authentication#authenticating-users) ---- ## 檢查有沒有 login ```php if (Auth::check()) { // The user is logged in... } ``` ---- ## Logout ```php Auth::logout(); ``` --- # 前後分離 ![Imgur](http://i.imgur.com/2vYQgo7.jpg) ---- # Many to Many ![Imgur](http://i.imgur.com/7mV94fw.jpg) ---- # Many to Many 好處 - Scale out - Load balance - 前後端分離,架構清晰 ---- ## Scale out vs. Scale up ![](http://www.ems5.com/uploads//2012-03/23/_1332473634_c5fdpm.jpg) *出處:http://www.ems5.com/view.php?id=308* ---- # 前後端分離該怎麼實現驗證機制? ---- # JWT ![Imgur](http://i.imgur.com/WQy7Ck6.jpg) https://jwt.io/ ---- # JWT - JWT - JSON Web Tokens - 由 Header, Payload, Signature 三者共同組成 - 詳細可以參考[JWT Introduction](https://jwt.io/introduction/) ---- # JWT Debugger ![Imgur](http://i.imgur.com/cTeaAHQ.jpg) https://jwt.io/ ---- # 當然 Laravel 也有 ![](https://cloud.githubusercontent.com/assets/1801923/9915273/119b9350-5cae-11e5-850b-c941cac60b32.png) https://github.com/tymondesigns/jwt-auth --- ## Laravel 實踐前後端配合 ![](http://laraveldaily.com/wp-content/uploads/2015/06/laravel-logo-big.png) ---- ## 我想...這個應該蠻常見 ```php 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 ```php 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](https://laravel.com/docs/5.1/controllers#restful-resource-controllers) ---- ## Laravel RestFul Controller 優點 - Route 簡潔又乾淨 - Controller 好追蹤 - 前端串接容易 ---- ![](https://idisciple.blob.core.windows.net/idm/Claim-Your-Victory.png) *出處:https://www.idisciple.org/post/claim-your-victory* --- # 但一定會不夠用 ```php 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](https://github.com/hashman/Laravel-Restful-demo)
{}
    1300 views