Skip to content

Instantly share code, notes, and snippets.

@cambiata
Created March 22, 2011 07:06
Show Gist options
  • Save cambiata/880880 to your computer and use it in GitHub Desktop.
Save cambiata/880880 to your computer and use it in GitHub Desktop.
Example controller extended from Controller_Cambiata_Rest
<?php
/**
* Test controller extending Controller_Cambiata_REST
* Jonas Nyström - cambiata
*
* To be used with a route like this one:
*
* Route::set('rest', 'rest(/<par1>(/<par2>(/<par3>(/<par4>(/<par5>)))))(.<format>)')
* ->defaults(array(
* 'controller' => 'testrest',
* 'action' => 'index',
* 'format' => 'xml',
* ));
*
*/
class Controller_TestRest extends Controller_Cambiata_REST {
private $parameter1;
private $parameter2;
private $parameter3;
public function before() {
parent::before();
//------------------------------------------------------------------------------
// Get some request parameters
$this->parameter1 = $this->request->param('par1');
$this->parameter2 = $this->request->param('par2');
$this->parameter3 = $this->request->param('par3');
//------------------------------------------------------------------------------
// Init authentication
//Cambiata_HttpAuth::basic_http_auth($this, 'basic_auth_callback');
// or...
//Cambiata_HttpAuth::header_token_auth($this, 'token_auth_callback', Cambiata_HttpAuth::X_AUTH_TOKEN);
}
//-----------------------------------------------------------------------------
// Authentication callbacks
/*
static public function basic_auth_callback($username, $password) {
// this method should be overridden
return true; //($username == 'jonas' AND $password = 'cambiata');
}
*/
// or...
/*
static public function token_auth_callback($token) {
// this method should be overridden
return ($token == 'abc123');
}
*/
//-------------------------------------------------------------
//-------------------------------------------------------------
//-------------------------------------------------------------
// GET
public function action_index(){
// Create some output data
$output_data = array('Hello' => 'World!');
$this->rest_output($output_data);
}
// POST
function action_create()
{
// Do whatever needed with incoming data:
// $this->input_content -> insert to database or something...
// Create some output data
$output_data = new StdClass();
$output_data->monitor_incoming = $this->input_content;
$output_data->test = 'ABC123';
$output_data->hobbies = array('Knitting', 'Cliffhanging');
$this->rest_output($output_data);
}
// PUT
function action_update()
{
// Do whatever needed with incoming data:
// $this->input_content -> update database or something...
$this->rest_output('PUT');
}
// DELETE
function action_delete()
{
$this->rest_output('DELETE');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment