Created
April 23, 2010 07:40
-
-
Save icyleaf/376317 to your computer and use it in GitHub Desktop.
RESTful by Kohana v3.0.x + Sprig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php defined('SYSPATH') or die('No direct script access.'); | |
/** | |
* People API - Example Code | |
* | |
* @author icyleaf <[email protected]> | |
* @link http://icyleaf.com | |
* @version 0.1 | |
*/ | |
class Controller_API_People extends Controller_REST { | |
/** | |
* Get user profile with GET request by id | |
* @param int $id | |
* @return void | |
*/ | |
public function action_index($id = NULL) | |
{ | |
if ( ! empty($id)) | |
{ | |
// Load user | |
$user = Sprig::factory('user', array('username' => $id)) | |
->load(); | |
if ($user->loaded()) | |
{ | |
// render data | |
$this->_render($user->as_array(), 200, TRUE); | |
} | |
else | |
{ | |
$this->_render('No Found the user: ' . $id, 404); | |
} | |
} | |
else | |
{ | |
$this->_render('Empty userID'); | |
} | |
} | |
/** | |
* Create one new user with POST request | |
* @return void | |
*/ | |
public function action_create() | |
{ | |
if ($_SERVER['REQUEST_METHOD'] == 'GET') | |
{ | |
// Load user | |
$id = Arr::get($_POST, 'username'); | |
$user = Sprig::factory('user', array('username' => $id)) | |
->load(); | |
if ( ! $user->loaded()) | |
{ | |
// create it! | |
$user = Sprig::factory('user') | |
->values($_POST) | |
->create(); | |
$this->_render('Saved', 201); | |
} | |
else | |
{ | |
$this->_render('Exist user', 403); | |
} | |
} | |
else | |
{ | |
$this->_render('No POST Request', 404); | |
} | |
} | |
/** | |
* Update exist user profile with PUT request by id | |
* @param int $id | |
* @return void | |
*/ | |
public function action_update($id = NULL) | |
{ | |
if ($_SERVER['REQUEST_METHOD'] == 'PUT') | |
{ | |
if ( ! empty($id)) | |
{ | |
// Load user | |
$user = Sprig::factory('user', array('username' => $id)) | |
->load(); | |
if ($user->loaded()) | |
{ | |
try | |
{ | |
// Assign PUT data | |
parse_str(file_get_contents("php://input"), $_PUT); | |
$_PUT = (count($_PUT) == 0) ? $_POST : $_PUT; | |
// Update user | |
$user->values($_PUT)->update(); | |
$this->_render('Updated', 202); | |
} | |
catch (Exception $e) | |
{ | |
$this->_render('Failed: ' . $e->getMessage(), 403); | |
} | |
} | |
else | |
{ | |
$this->_render('No Found the user: ' . $id, 404); | |
} | |
} | |
else | |
{ | |
$this->_render('Empty userID'); | |
} | |
} | |
else | |
{ | |
$this->_render('No PUT Request', 404); | |
} | |
} | |
/** | |
* Delete a exist user with DELETT request by id | |
* @param int $id | |
* @return void | |
*/ | |
public function action_delete($id = NULL) | |
{ | |
if ($_SERVER['REQUEST_METHOD'] == 'DELETE') | |
{ | |
if ( ! empty($id)) | |
{ | |
// Load user | |
$user = Sprig::factory('user', array('username' => $id)) | |
->load(); | |
if ($user->loaded()) | |
{ | |
// delete it! | |
$user->delete(); | |
$this->_render('Deleted'); | |
} | |
else | |
{ | |
$this->_render('No Found the user: ' . $id, 404); | |
} | |
} | |
else | |
{ | |
$this->_render('Empty userID'); | |
} | |
} | |
else | |
{ | |
$this->_render('No DELETE Request', 404); | |
} | |
} | |
/** | |
* Render the response message with status and content-type | |
* @param sting $content | |
* @param int $status | |
* @param boolean $format | |
* @return void | |
*/ | |
private function _render($content, $status = 200, $format = FALSE) | |
{ | |
$header = array(); | |
if ($format) | |
{ | |
$alt = Arr::get($_GET, 'alt', 'json'); | |
switch($alt) | |
{ | |
case 'xml': | |
// TODO: format $content into xml and assign itself | |
$header = array( | |
'Content-Type' => 'text/xml; charset=utf-8' | |
); | |
break; | |
default: | |
case 'json': | |
$header = array( | |
'Content-Type' => 'application/json; charset=utf-8' | |
); | |
$content = json_encode($content); | |
break; | |
} | |
} | |
$this->request->headers = $header; | |
$this->request->status = $status; | |
$this->request->response = $content; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
== Route ==
Route::set('api/people', 'api/people(/)')
->defaults(array(
'directory' => 'api',
'controller' => 'people',
'action' => 'index',
));
== Model ==
models is used Sprig modul, stored in classes/model/user.php