This document discusses implementing a RESTful API with CodeIgniter PHP framework. It begins with an introduction to RESTful APIs and best practices for API design. It then covers using the CodeIgniter REST Server library to create CRUD API endpoints for resources like topics. Examples are given for API requests like create, read, update and delete. It also discusses verifying the API with the CodeIgniter REST Client. The goal is to demonstrate how to easily create a RESTful API with CodeIgniter following REST standards.
57. 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
61. 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() {}
}
62. 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() {}
}
82. 2012 PHPConf 82
Load Library
// Load the rest client spark
$this->load->spark('restclient/2.1.0');
// Load the library
$this->load->library('rest');
83. 2012 PHPConf 83
Setup API Server
// Run some setup
$this->rest->initial('xxxxxx');
// twitter server
$this->load->initial('http://twitter.com');
84. 2012 PHPConf 84
Parameter
// set api path
$api = '/API/Topic/Add';
// set api data
$data = array(
'title' => 'I am Kate Upton',
'type' => 'girl'
);
85. 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);
91. 2012 PHPConf 91
class Home_Controller extends Base_Controller
{
public $restful = true;
public function get_index()
{
//
}
public function post_index()
{
//
}
}
92. 2012 PHPConf 92
More Introduction to Laravel Framework
14:20 – 14:50
用 Laravel Framework 打造現代化網站應用程式
大澤木小鐵