-
-
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.
This file contains hidden or 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 | |
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