Skip to content

Instantly share code, notes, and snippets.

@danmatthews
Created August 2, 2012 19:49
Show Gist options
  • Save danmatthews/3240064 to your computer and use it in GitHub Desktop.
Save danmatthews/3240064 to your computer and use it in GitHub Desktop.
Solution to doing some processing for every method of FuelPHP REST controller.
<?php
class ExampleRestController extends Controller_Rest {
/**
* 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) {
// Run a method that validates the provided API key.
if (!static::validateMyAPIKey())
{
// If it has not passed validation return a FuelPHP Response object.
$response = Response::forge(Format::forge(array('Error' => 'Failed to authenticate you'))->to_json(), 401)->set_header('Content-Type', 'application.json');
return $response;
}
// The API key is valid, Route the request!
parent::router($method, $params);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment