Skip to content

Instantly share code, notes, and snippets.

@ariefbayu
Forked from danmatthews/gist:3240064
Created October 9, 2012 09:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ariefbayu/3857511 to your computer and use it in GitHub Desktop.
Save ariefbayu/3857511 to your computer and use it in GitHub Desktop.
Solution to doing some processing for every method of FuelPHP REST controller. Also add limit supported data type.
<?php
class ExampleRestController extends Controller_Rest {
/**
* @var array List all supported methods
*/
protected $_supported_formats = array(
'xml' => 'application/xml',
// 'rawxml' => 'application/xml',
'json' => 'application/json',
// 'jsonp'=> 'text/javascript',
// 'serialized' => 'application/vnd.php.serialized',
// 'php' => 'text/plain',
// 'html' => 'text/html',
// 'csv' => 'application/csv',
);
/**
* Make sure the router method has the same method signature as the parent::router declaration.
* @see fuel/core/classes/controller/rest.php
*/
public function router($method, array $params) {
$defaultResponse = array('resp'=>'NG', 'status'=>'authentication key doesn\'t recognized.');
if(!static::validateMyAPIKey()){
if($this->format == "json"){
$response = Response::forge(Format::forge( $defaultResponse )->to_json())
->set_header('Content-Type', $this->_supported_formats[$this->format]);
} else if($this->format == $xml) {
$response = Response::forge(Format::forge( $defaultResponse )->to_xml())
->set_header('Content-Type', $this->_supported_formats[$this->format]);
}
return $response;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment