From b245e3475f66c94afb94f8b2287bf0185a343732 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Thu, 28 May 2009 06:07:27 +0800 Subject: Restructure things so that the application is now just another module. Kohana makes this type of transition fairly straightforward in that all controllers/helpers/etc are still located in the cascading filesystem without any extra effort, except that I've temporarily added a hack to force modules/gallery into the module path. Rename what's left of "core" to be "application" so that it conforms more closely to the Kohana standard (basically, just application/config/config.php which is the minimal thing that you need in the application directory) There's still considerable work left to be done here. Signed-off-by: Gallery Role Account --- modules/gallery/controllers/rest.php | 183 +++++++++++++++++++++++++++++++++++ 1 file changed, 183 insertions(+) create mode 100644 modules/gallery/controllers/rest.php (limited to 'modules/gallery/controllers/rest.php') diff --git a/modules/gallery/controllers/rest.php b/modules/gallery/controllers/rest.php new file mode 100644 index 00000000..11a6bbac --- /dev/null +++ b/modules/gallery/controllers/rest.php @@ -0,0 +1,183 @@ +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(); + } + + $resource = ORM::factory($this->resource_type, (int)$function); + if (!$resource->loaded && $request_method != "post") { + return Kohana::show_404(); + } + + if ($request_method != "get") { + access::verify_csrf(); + } + + switch ($request_method) { + case "get": + return $this->_show($resource); + + 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) + */ + public function _index() { + throw new Exception("@todo _create NOT IMPLEMENTED"); + } + + /** + * Perform a POST request on this resource + * @param ORM $resource the instance of this resource type + */ + public function _create($resource) { + throw new Exception("@todo _create NOT IMPLEMENTED"); + } + + /** + * Perform a GET request on this resource + * @param ORM $resource the instance of this resource type + */ + public function _show($resource) { + throw new Exception("@todo _show NOT IMPLEMENTED"); + } + + /** + * Perform a PUT request on this resource + * @param ORM $resource the instance of this resource type + */ + public function _update($resource) { + throw new Exception("@todo _update NOT IMPLEMENTED"); + } + + /** + * Perform a DELETE request on this resource + * @param ORM $resource the instance of this resource type + */ + public function _delete($resource) { + throw new Exception("@todo _delete NOT IMPLEMENTED"); + } + + /** + * Present a form for adding a new resource + * @param string part of the URI after the controller name + */ + public function _form_add($parameter) { + throw new Exception("@todo _form_add NOT IMPLEMENTED"); + } + + /** + * Present a form for editing an existing resource + * @param ORM $resource the resource container for instances of this resource type + */ + public function _form_edit($resource) { + throw new Exception("@todo _form_edit NOT IMPLEMENTED"); + } +} -- cgit v1.2.3