Skip to content

Commit

Permalink
added execute() method that throws 405 instead of the default 404
Browse files Browse the repository at this point in the history
  • Loading branch information
michal-m committed Mar 10, 2013
1 parent 14b7fac commit d4444bd
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions classes/RESTful/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,46 @@ abstract class RESTful_Controller extends Controller {
*/
protected $_request_data;

/**
* Executes the given action and calls the [Controller::before] and [Controller::after] methods.
*
* Can also be used to catch exceptions from actions in a single place.
*
* 1. Before the controller action is called, the [Controller::before] method
* will be called.
* 2. Next the controller action will be called.
* 3. After the controller action is called, the [Controller::after] method
* will be called.
*
* @throws HTTP_Exception_405
* @return Response
*/
public function execute()
{
// Execute the "before action" method
$this->before();

// Determine the action to use
$action = 'action_'.$this->request->action();

// If the action doesn't exist, it's a 405 Method Not Allowed
if ( ! method_exists($this, $action))
{
throw HTTP_Exception::factory(405)
->headers('Allow', implode(', ', array_keys($this->_action_map)))
->request($this->request);
}

// Execute the action itself
$this->{$action}();

// Execute the "after action" method
$this->after();

// Return the response
return $this->response;
}

/**
* Preflight checks.
*/
Expand Down

0 comments on commit d4444bd

Please sign in to comment.