From dae36c2aa4afebb38cc3235b46c6490b2f771aa1 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Tue, 11 Nov 2008 06:18:45 +0000 Subject: Create REST_Controller abstract base class for all REST based resource controllers. Any controller that wants to act RESTful can extend this class and implement get/post/put/delete. Tweak default routes to disallow direct access to the REST controller and direct access to any REST methods. --- core/controllers/rest.php | 83 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 core/controllers/rest.php (limited to 'core/controllers/rest.php') diff --git a/core/controllers/rest.php b/core/controllers/rest.php new file mode 100644 index 00000000..50e4e113 --- /dev/null +++ b/core/controllers/rest.php @@ -0,0 +1,83 @@ +resource_type == null) { + throw new Exception("@todo ERROR_MISSING_RESOURCE_TYPE"); + } + + // @todo this needs security checks + $resource = ORM::factory($this->resource_type)->where("id", $id)->find(); + if (!$resource->loaded) { + return Kohana::show_404(); + } + + /** + * We're expecting to run in an environment that only supports GET/POST, so expect to tunnel + * PUT/DELETE through POST. + */ + if (request::method() == "get") { + $this->get($resource); + + if (Session::instance()->get("use_profiler", false)) { + $profiler = new Profiler(); + print $profiler->render(); + } + return; + } + + switch ($this->input->post("__action")) { + case "put": + return $this->put($resource); + + case "delete": + return $this->delete($resource); + + default: + return $this->post($resource); + } + } + + /** + * Perform a GET request on this resource + * @param ORM $resource the instance of this resource type + */ + abstract public function get($resource); + + /** + * Perform a PUT request on this resource + * @param ORM $resource the instance of this resource type + */ + abstract public function put($resource); + + /** + * Perform a POST request on this resource + * @param ORM $resource the instance of this resource type + */ + abstract public function post($resource); + + /** + * Perform a DELETE request on this resource + * @param ORM $resource the instance of this resource type + */ + abstract public function delete($resource); +} -- cgit v1.2.3