Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
2012 Taiwain PHPConf RESTful API Design & Imple...
Search
Bo-Yi Wu
November 03, 2012
Technology
6
340
2012 Taiwain PHPConf RESTful API Design & Implementation with CodeIgniter PHP Framework
Introduction to RESTFul API with CodeIgniter or Laravel PHP Framework
Bo-Yi Wu
November 03, 2012
Tweet
Share
More Decks by Bo-Yi Wu
See All by Bo-Yi Wu
Building a Unified API Gateway for Secure and Scalable Cross-Cloud AI Service
appleboy
0
110
Building MCP (Model Context Protocol) with Golang
appleboy
0
530
如何設計一套具備 Container 容器化技術的 CI/CD 平台?
appleboy
0
1.5k
生成式 AI CodeGPT 開發經驗談
appleboy
0
3k
打造 MLOps 平台 改善 AI 模型開發流程
appleboy
0
2.2k
自動化監控伺服器工具 - Gatus
appleboy
0
4k
Drone CI/CD 自動化測試及部署
appleboy
1
510
初探 Infrastructure as Code 工具 Pulumi
appleboy
2
3.6k
Introduction to Open Policy Agent
appleboy
0
2k
Other Decks in Technology
See All in Technology
AWS CDK 入門ガイド これだけは知っておきたいヒント集
anank
4
490
いつの間にか入れ替わってる!?新しいAWS Security Hubとは?
cmusudakeisuke
0
150
敢えて生成AIを使わないマネジメント業務
kzkmaeda
2
500
Four Keysから始める信頼性の改善 - SRE NEXT 2025
ozakikota
0
130
cdk initで生成されるあのファイル達は何なのか/cdk-init-generated-files
tomoki10
1
430
助けて! XからWaylandに移行しないと新しいGNOMEが使えなくなっちゃう 2025-07-12
nobutomurata
2
130
データ基盤からデータベースまで?広がるユースケースのDatabricksについて教えるよ!
akuwano
3
150
United™️ Airlines®️ Customer®️ USA Contact Numbers: Complete 2025 Support Guide
flyunitedguide
0
720
オフィスビルを監視しよう:フィジカル×デジタルにまたがるSLI/SLO設計と運用の難しさ / Monitoring Office Buildings: The Challenge of Physical-Digital SLI/SLO Design & Operation
bitkey
1
290
QuickSight SPICE の効果的な運用戦略~S3 + Athena 構成での実践ノウハウ~/quicksight-spice-s3-athena-best-practices
emiki
0
230
Amplify Gen2から知るAWS CDK Toolkit Libraryの使い方/How to use the AWS CDK Toolkit Library as known from Amplify Gen2
fossamagna
0
150
マルチプロダクト環境におけるSREの役割 / SRE NEXT 2025 lunch session
sugamasao
1
220
Featured
See All Featured
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
18
980
GitHub's CSS Performance
jonrohan
1031
460k
jQuery: Nuts, Bolts and Bling
dougneiner
63
7.8k
Intergalactic Javascript Robots from Outer Space
tanoku
271
27k
Being A Developer After 40
akosma
90
590k
Music & Morning Musume
bryan
46
6.7k
A better future with KSS
kneath
238
17k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
8
700
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
7
740
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
34
5.9k
Measuring & Analyzing Core Web Vitals
bluesmoon
7
510
Making the Leap to Tech Lead
cromwellryan
134
9.4k
Transcript
RESTful API Design & Implementation with CodeIgniter PHP Framework 2012
PHP Conference
2012 PHPConf 2 Who Am I Bo-Yi Wu @appleboy http://blog.wu-boy.com
任職於瑞昱半導體 RealTek(IC Design House) - TV 多媒體部門 - Sencha Touch 2, Backbone.js, CodeIgniter, Node.js, MongoDB, MySQL, Twitter Bootstrap, Twitter Hogan ...
2012 PHPConf 3 Who Am I • Open Source Contributions
(github: appleboy) – CodeIgniter-Native-Session – CodeIgniter-i18n – CodeIgniter-Template – CodeIgniter-Nexmo-Message – CodeIgniter-TextMagic-API
2012 PHPConf 4 My Focus • CodeIgbiter 3.0.x develop branch
– Support Native Session – Support HMVC – Support Sparks Package Management System • Laravel develop branch • Javascript (Node.js, Socket.io, Express, Backbone.js)
2012 PHPConf 5 Outline • Restful API Basic • API
Design • Implementing API with CodeIgniter • Verify your API
2012 PHPConf 6 Restful API Basic Restful API Basic
2012 PHPConf 7 Why Use Restful? Restful API Service Database
(MySQL,MongoDB)
2012 PHPConf 8 What is REST? Http Method • POST
• GET • PUT • DELETE • OPTIONS Define in RFC 2616
2012 PHPConf 9 並非所有的瀏覽器都支援 PUT, DELETE http://api.jquery.com/jQuery.ajax/
2012 PHPConf 10 如何解決未支援的瀏覽器
2012 PHPConf 11 <input type='hidden' name='type' value='PUT'>
2012 PHPConf 12 將PUT,DELETE一併寫成POST API
2012 PHPConf 13 REST Response Format? • JSON* • XML
• Array • Html • CSV
2012 PHPConf 14 現在皆以 JSON JSON 為主 格式簡單 , 相容性高
, 閱讀方便
2012 PHPConf 15 Javascript Object Notation { key1: 'value1', key2:
20121103 key3: [1,2,3] }
2012 PHPConf 16 JSON in Javascript is familiar var object
= { key1: 'value1', key2: 20121103 key3: [1,2,3] }
2012 PHPConf 17 JSON in PHP (encode) PHP <?php echo
json_encode(array( 'key' => 'value' )); Outputs {key: 'value'}
2012 PHPConf 18 JSON in PHP (decode) PHP <?php $json_data
= '{key: value}'; echo json_decode({ 'key' => 'value' }); Outputs array( 'key' => 'value' );
2012 PHPConf 19 你不可不知的 JSON JSON 基本介紹 http://goo.gl/Wvhwb
2012 PHPConf 20 API Design API Design
2012 PHPConf 21 良好的 API 設計 • Simple 簡單 •
Intuitive 直觀的 • Stable 穩定 • Well Document 線上文件
2012 PHPConf 22 Using Facebook API Using Facebook API
2012 PHPConf 23 Fucking Stable and Document Fucking Stable and
Document
2012 PHPConf 24 請務必撰寫 API API 線上文件
2012 PHPConf 25 大家每天在花在討論的時間太長 Debug 時間變少
2012 PHPConf 26 良好的文件減少人與人溝通成本 團隊合作
2012 PHPConf 27 Http Method RFC 2616 • Create •
Read • Update • Delete • POST • GET • PUT • DELETE CRUD Method
2012 PHPConf 28 API URL Define API URL Define
2012 PHPConf 29 /API/ /API/Module Module/ /Method Method
2012 PHPConf 30 Format 1: Topic Module • /API/Topic/Add •
/API/Topic/Update • /API/Topic/Delete • /API/Topic/List
2012 PHPConf 31 Format 2: Topic Module • /API/Topic/Add •
/API/Topic/Update/1234 • /API/Topic/Delete/1234 • /API/Topic/List/sort/asc
2012 PHPConf 32 個人偏好格式 1
2012 PHPConf 33 不用記住多種不同 API API 格式
2012 PHPConf 34 API Response Format API Response Format
2012 PHPConf 35 請勿常常修改 format format ( 除非你想黑掉 )
2012 PHPConf 36 Example Create API var object = {
title: 'value1', type: 'value2', user_id: '1000' }; Input Output { title: 'value1', type: 'value2', user_id: '1000', success_text: 'ok' } http://site.com/API/Topic/Add
2012 PHPConf 37 Example Create API var object = {
title: 'value1', type: 'value2' }; Input Output { title: 'value1', type: 'value2', user_id: '1000', success_text: 'ok' } http://site.com/API/Topic/Add/1000
2012 PHPConf 38 Example Update API var object = {
id: '1000', title: 'value1', type: 'value2' }; Input Output { id: '1000', title: 'value1', type: 'value2', success_text: 'ok' } http://site.com/API/Topic/Update
2012 PHPConf 39 Example Update API var object = {
title: 'value1', type: 'value2' }; Input Output { id: '1000', title: 'value1', type: 'value2', success_text: 'ok' } http://site.com/API/Topic/Update/1000
2012 PHPConf 40 Example Delete API (single) var object =
{ id: 1000 }; Input Output { id: '1000', success_text: 'ok' } http://site.com/API/Topic/Delete
2012 PHPConf 41 Example Delete API (multiple) var object =
{ id: [1000, 1001] }; Input Output { id: '1000', success_text: 'ok' } http://site.com/API/Topic/Delete
2012 PHPConf 42 Example Delete API var object = {
}; Input Output { id: '1000', success_text: 'ok' } http://site.com/API/Topic/Delete/1000
2012 PHPConf 43 Example Read API (Single) var object =
{ id: 1000 }; Input Output { id: '1000', success_text: 'ok', item: { title: 'Kate Upton' } } http://site.com/API/Topic/List
2012 PHPConf 44 Example Search API (Multiple) var object =
{ q: 'Kate Upton' }; Input Output { id: '1000', success_text: 'ok', items: [ {title: 'I am kate'}, {title: 'I am Upton'} ] } http://site.com/API/Topic/List
2012 PHPConf 45 Kate Upton
2012 PHPConf 46 多虧了Youtube API Youtube API讓我在上班時增加了很多動力
2012 PHPConf 47 How to handle versioning? How to handle
versioning?
2012 PHPConf 48 內部 API API 大改版
2012 PHPConf 49 Old: http://site.com/v1/API/Topic/Add New: http://site.com/v2/API/Topic/Add
2012 PHPConf 50 利用 URI Routing URI Routing 功能 Framework
or mod_rewrite Framework or mod_rewrite
2012 PHPConf 51 http://site.com/API/Topic/Add http://site.com/v1/API/Topic/Add =
2012 PHPConf 52 API Implementation API Implementation
2012 PHPConf 53 不用自己造輪子
2012 PHPConf 54 Phil Sturgeon’s CodeIgniter REST Server http://github.com/philsturgeon/codeigniter-restserver
2012 PHPConf 55 Requirements • PHP 5.2 or greater •
CodeIgniter 2.1.x to 3.0-dev
2012 PHPConf 56 How to install? How to install?
2012 PHPConf 57 Installation • Drag and drop the following
files into your application's directories – application/libraries/Format.php – application/libraries/REST_Controller.php – application/config/rest.php
2012 PHPConf 58 Setup the config • $config['rest_default_format'] = 'json';
• $config['rest_enable_keys'] = false; • $config['rest_enable_logging'] = false; • $config['rest_enable_limits'] = false; • $config['rest_ajax_only'] = false;
2012 PHPConf 59 Include REST Controller Include REST Controller
2012 PHPConf 60 require(APPPATH.'/libraries/REST_Controller.php');
2012 PHPConf 61 Handling Requests class Topic extends REST_Controller {
public function index_get() {} public function index_post() {} public function index_update() {} public function index_delete() {} }
2012 PHPConf 62 CRUD Requests class Topic extends REST_Controller {
public function list_get() {} public function add_post() {} public function update_update() {} public function delete_delete() {} }
2012 PHPConf 63 Accessing parameters is also easy Accessing parameters
is also easy
2012 PHPConf 64 Parameters • GET – $this->get('blah'); • POST
– $this->post('blah'); • UPDATE – $this->update('blah'); • DELETE – $this->delete('blah');
2012 PHPConf 65 Create API var object = { title:
'Kate Upton', text: 'Beautiful girl' }; Input Output { id: '1000', success_text: 'ok', } http://site.com/API/Topic/Add
2012 PHPConf 66 Create API (POST) public function Add_post() {
if (!$this->post('title')) { $this->response(array('error' => 'Title is required'), 404); } $output = $this->lib_topic->insert($data); if ($output) { $this->response($output, 200); } else { $this->response(array('error' => 'Insert error'), 404); } }
2012 PHPConf 67 Update API var object = { id:
1000, title: 'Kate Upton', text: 'Beautiful girl' }; Input Output { id: '1000', success_text: 'ok', } http://site.com/API/Topic/Update
2012 PHPConf 68 Update API (PUT) public function Update_put() {
if (!$this->update('id')) { $this->response(array('error' => 'ID is required'), 404); } $output = $this->lib_topic->update($this->update('id'), $data); if ($output) { $this->response($output, 200); } else { $this->response(array('error' => 'Insert error'), 404); } }
2012 PHPConf 69 Delete API var object = { id:
1000 }; Input Output { id: '1000', success_text: 'ok', } http://site.com/API/Topic/Delete
2012 PHPConf 70 Delete API (DELETE) public function Delete_delete() {
if (!$this->delete('id')) { $this->response(array('error' => 'ID is required'), 404); } $output = $this->lib_topic->delete($this->delete('id')); if ($output) { $this->response($output, 200); } else { $this->response(array('error' => 'Insert error'), 404); } }
2012 PHPConf 71 Read API (GET) var object = {
id: 1000, type: [1, 2] }; Input Output { id: '1000', success_text: 'ok', item: { title: 'Kate Upton' } } http://site.com/API/Topic/List
2012 PHPConf 72 Read API (GET) public function List_get() {
if (!$this->get('id') or ) { $this->response(array('error' => 'ID is required'), 404); } $output = $this->lib_topic->list($this->get('id'), $this->get('type')); if ($output) { $this->response($output, 200); } else { $this->response(array('error' => 'Insert error'), 404); } }
2012 PHPConf 73 目錄結構
2012 PHPConf 74 Folder application controllers/ api/ topic.php user.php acl.php
system index.php
2012 PHPConf 75 Routing (config/routes.php) Default URL http://site.com/api/topic/Add New URL
http://site.com/API/Topic/Add $route['API/Topic/(:any)'] = 'api/topic/$1'; $route['API/User/(:any)'] = 'api/user/$1'; $route['API/Acl/(:any)'] = 'api/acl/$1';
2012 PHPConf 76 Verify Your API Verify Your API
2012 PHPConf 77 一樣不需要自己造輪子
2012 PHPConf 78 Phil Sturgeon’s CodeIgniter REST Client https://github.com/philsturgeon/codeigniter-restclient
2012 PHPConf 79 Requirements Requirements
2012 PHPConf 80 Requirements • PHP 5.1+ • CodeIgniter 2.0.0+
• CURL • CodeIgniter Curl library: http://getsparks.org/packages/curl/show
2012 PHPConf 81 Load Rest Client Library Load Rest Client
Library
2012 PHPConf 82 Load Library // Load the rest client
spark $this->load->spark('restclient/2.1.0'); // Load the library $this->load->library('rest');
2012 PHPConf 83 Setup API Server // Run some setup
$this->rest->initial('xxxxxx'); // twitter server $this->load->initial('http://twitter.com');
2012 PHPConf 84 Parameter // set api path $api =
'/API/Topic/Add'; // set api data $data = array( 'title' => 'I am Kate Upton', 'type' => 'girl' );
2012 PHPConf 85 Test it // GET API $this->rest->get($api, $data);
// POST API $this->rest->post($api, $data); // UPDATE API $this->rest->update($api, $data); // DELETE API $this->rest->delete($api, $data);
2012 PHPConf 86 $this->rest->debug(); Rest Client Library debug mode
2012 PHPConf 87 以上是CodeIgniter PHP Framework
2012 PHPConf 88 Implement another Framework? Implement another Framework?
2012 PHPConf 89 Laravel PHP Framework? Laravel PHP Framework?
2012 PHPConf 90 public $restful = true;
2012 PHPConf 91 class Home_Controller extends Base_Controller { public $restful
= true; public function get_index() { // } public function post_index() { // } }
2012 PHPConf 92 More Introduction to Laravel Framework 14:20 –
14:50 用 Laravel Framework 打造現代化網站應用程式 大澤木小鐵
2012 PHPConf 93 RESTful API 就講到這裡
2012 PHPConf 94 如果有任何問題
2012 PHPConf 95 可以上 CodeIgniter CodeIgniter 論壇
2012 PHPConf 96 http://www.codeigniter.org.tw/forum/
2012 PHPConf 97 謝謝大家及工作團隊