resource_type == null) { throw new Exception("@todo ERROR_MISSING_RESOURCE_TYPE"); } parent::__construct(); } /** * Handle dispatching for all REST controllers. */ public function __call($function, $args) { // If no parameter was provided after the controller name (eg "/albums") then $function will // be set to "index". Otherwise, $function is the first parameter, and $args are all // subsequent parameters. $request_method = rest::request_method(); if ($function == "index" && $request_method == "get") { return $this->_index(); } // @todo this needs security checks $id = $function; $resource = ORM::factory($this->resource_type, $id); if (!$resource->loaded && $request_method != "post") { return Kohana::show_404(); } switch ($request_method) { case "get": $this->_show($resource); if (Session::instance()->get("use_profiler", false)) { $profiler = new Profiler(); $profiler->render(); } return; case "put": return $this->_update($resource); case "delete": return $this->_delete($resource); case "post": return $this->_create($resource); } } /* We're editing an existing item, load it from the database. */ public function form_edit($resource_id) { if ($this->resource_type == null) { throw new Exception("@todo ERROR_MISSING_RESOURCE_TYPE"); } // @todo this needs security checks $resource = ORM::factory($this->resource_type, $resource_id); if (!$resource->loaded) { return Kohana::show_404(); } return $this->_form_edit($resource); } /* We're adding a new item, pass along any additional parameters. */ public function form_add($parameters) { return $this->_form_add($parameters); } /** * Perform a GET request on the controller root * (e.g. http://www.example.com/gallery3/comments) */ abstract public function _index(); /** * Perform a POST request on this resource * @param ORM $resource the instance of this resource type */ abstract public function _create($resource); /** * Perform a GET request on this resource * @param ORM $resource the instance of this resource type */ abstract public function _show($resource); /** * Perform a PUT request on this resource * @param ORM $resource the instance of this resource type */ abstract public function _update($resource); /** * Perform a DELETE request on this resource * @param ORM $resource the instance of this resource type */ abstract public function _delete($resource); /** * Present a form for adding a new resource * @param string part of the URI after the controller name */ abstract public function _form_add($parameter); /** * Present a form for editing an existing resource * @param ORM $resource the resource container for instances of this resource type */ abstract public function _form_edit($resource); }