summaryrefslogtreecommitdiff
path: root/core/controllers
diff options
context:
space:
mode:
authorJozef Selesi <jozefs@users.sourceforge.net>2008-11-18 23:40:47 +0000
committerJozef Selesi <jozefs@users.sourceforge.net>2008-11-18 23:40:47 +0000
commit1992343c2e1f2df892f39f971ae629edc59c33d0 (patch)
tree4d0e6365789905cf702324bc77b104a8eeaecb39 /core/controllers
parent4bf486955cea9e2b67531f044c6bf34a7bc9324f (diff)
* Changed REST API. Now there are two separate methods for forms:
GET /form/edit/{controller}/{resource_id} -> controller::_form_edit($resource) GET /form/add/{controller}/{parameters} -> controller::_form_add($parameters) * Updated comment, user and core modules to reflect the API changes * Cleaned up routing and handling of requests to /{controller}
Diffstat (limited to 'core/controllers')
-rw-r--r--core/controllers/item.php11
-rw-r--r--core/controllers/rest.php60
2 files changed, 43 insertions, 28 deletions
diff --git a/core/controllers/item.php b/core/controllers/item.php
index 1113d00f..f5c5f9c9 100644
--- a/core/controllers/item.php
+++ b/core/controllers/item.php
@@ -28,9 +28,16 @@ class Item_Controller extends REST_Controller {
}
/**
- * @see Rest_Controller::_form($resource)
+ * @see Rest_Controller::_form_add($parameters)
*/
- public function _form($item, $form_type) {
+ public function _form_add($parameters) {
+ throw new Exception("@todo Comment_Controller::_form NOT IMPLEMENTED");
+ }
+
+ /**
+ * @see Rest_Controller::_form_edit($resource)
+ */
+ public function _form_edit($item) {
throw new Exception("@todo Comment_Controller::_form NOT IMPLEMENTED");
}
diff --git a/core/controllers/rest.php b/core/controllers/rest.php
index 30a92b24..937924cd 100644
--- a/core/controllers/rest.php
+++ b/core/controllers/rest.php
@@ -25,29 +25,34 @@
* protected $resource_type = "comment"; // this tells REST which model to use
*
* public function _index() {
- * // Handle GET request to controller root
+ * // Handle GET request to /controller
* }
*
* public function _show(ORM $comment, $output_format) {
- * // Handle GET request
+ * // Handle GET request to /comments/{comment_id}
* }
*
* public function _update(ORM $comment, $output_format) {
- * // Handle PUT request
+ * // Handle PUT request to /comments/{comment_id}
* }
*
* public function _create(ORM $comment, $output_format) {
- * // Handle POST request to controller root
+ * // Handle POST request to /comments
* }
*
* public function _delete(ORM $comment, $output_format) {
- * // Handle DELETE request
+ * // Handle DELETE request to /comments/{comments_id}
* }
*
- * public function form(ORM $comment) {
+ * public function _form_add($parameters) {
+ * // Handle GET request to /form/add/comments
* // Show a form for creating a new comment
* }
- * }
+ *
+ * public function _form_edit(ORM $comment) {
+ * // Handle GET request to /form/edit/comments
+ * // Show a form for editing an existing comment
+ * }
*
* A request to http://example.com/gallery3/comments/3 will result in a call to
* REST_Controller::dispatch(3) which will load up the comment associated with id 3. If there's
@@ -62,6 +67,11 @@ abstract class REST_Controller extends Controller {
throw new Exception("@todo ERROR_MISSING_RESOURCE_TYPE");
}
+ /* A request on /controller gets routed to REST_Controller::dispatch(0). */
+ if ($id == 0 && $this->request_method() == "get") {
+ return $this->_index();
+ }
+
// @todo this needs security checks
$resource = ORM::factory($this->resource_type, $id);
if (!$resource->loaded && !$this->request_method() == "post") {
@@ -90,32 +100,24 @@ abstract class REST_Controller extends Controller {
}
}
- // @todo Get rid of $form_type, move to add_form() and edit_form().
- public function form($data, $form_type) {
+ /* 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
- if ($form_type == "edit") {
- /* We're editing an existing item, load it from the database. */
- $resource = ORM::factory($this->resource_type, $data);
- if (!$resource->loaded) {
- return Kohana::show_404();
- }
-
- return $this->_form($resource, $form_type);
- } else {
- /* We're adding a new item, pass along any additional parameters. */
- return $this->_form($data, $form_type);
+ $resource = ORM::factory($this->resource_type, $resource_id);
+ if (!$resource->loaded) {
+ return Kohana::show_404();
}
+
+ return $this->_form_edit($resource);
}
- public function index() {
- if (request::method() == "post") {
- return $this->dispatch(null);
- }
- return $this->_index();
+ /* We're adding a new item, pass along any additional parameters. */
+ public function form_add($parameters) {
+ return $this->_form_add($parameters);
}
/**
@@ -175,7 +177,13 @@ abstract class REST_Controller extends Controller {
/**
* 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($resource, $form_type);
+ abstract public function _form_edit($resource);
}