Skip to content

Instantly share code, notes, and snippets.

@icyleaf
Created April 23, 2010 07:40
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save icyleaf/376317 to your computer and use it in GitHub Desktop.
Save icyleaf/376317 to your computer and use it in GitHub Desktop.
RESTful by Kohana v3.0.x + Sprig
<?php defined('SYSPATH') or die('No direct script access.');
/**
* People API - Example Code
*
* @author icyleaf <icyleaf.cn@gmail.com>
* @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;
}
}
@icyleaf
Copy link
Author

icyleaf commented Apr 23, 2010

== Route ==
Route::set('api/people', 'api/people(/)')
->defaults(array(
'directory' => 'api',
'controller' => 'people',
'action' => 'index',
));

Route::set('api', 'api/(<controller>(/<action>(/<id>)))')
    ->defaults(array(
        'directory'  => 'api',
    ));

== Model ==

models is used Sprig modul, stored in classes/model/user.php

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment