diff options
-rw-r--r-- | core/config/routes.php | 4 | ||||
-rw-r--r-- | core/controllers/item.php | 11 | ||||
-rw-r--r-- | core/controllers/rest.php | 60 | ||||
-rw-r--r-- | modules/comment/controllers/comments.php | 20 | ||||
-rw-r--r-- | modules/user/controllers/users.php | 17 |
5 files changed, 66 insertions, 46 deletions
diff --git a/core/config/routes.php b/core/config/routes.php index ea177eda..120e6900 100644 --- a/core/config/routes.php +++ b/core/config/routes.php @@ -24,8 +24,8 @@ $config['^rest'] = null; $config['^rest/.*'] = null; $config['^(\w+)/(\d+)$'] = '$1/dispatch/$2'; -$config['^(\w+)$'] = '$1/index'; -$config['^form/(\w+)/(\w+)/(.*)$'] = '$2/form/$3/$1'; +$config['^(\w+)$'] = '$1/dispatch/0'; +$config['^form/(edit|add)/(\w+)/(.*)$'] = '$2/form_$1/$3'; // For now our default page is the scaffolding. $config['_default'] = 'welcome'; 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); } diff --git a/modules/comment/controllers/comments.php b/modules/comment/controllers/comments.php index 34557a83..89e42753 100644 --- a/modules/comment/controllers/comments.php +++ b/modules/comment/controllers/comments.php @@ -107,15 +107,17 @@ class Comments_Controller extends REST_Controller { /** * Present a form for adding a new comment to this item or editing an existing comment. - * @see Rest_Controller::form($resource) + * @see Rest_Controller::form_add($resource) */ - public function _form($resource, $form_type) { - // This code will be clearer if we split form() into two functions. - if ($form_type == "edit") { - $form = comment::get_edit_form($resource); - } else { - $form = comment::get_add_form($resource); - } - print $form; + public function _form_add($item_id) { + print comment::get_add_form($item_id); + } + + /** + * Present a form for editing an existing comment. + * @see Rest_Controller::form_edit($resource) + */ + public function _form_edit($comment) { + print $form = comment::get_edit_form($comment); } } diff --git a/modules/user/controllers/users.php b/modules/user/controllers/users.php index d381d399..871af942 100644 --- a/modules/user/controllers/users.php +++ b/modules/user/controllers/users.php @@ -71,12 +71,15 @@ class Users_Controller extends REST_Controller { * Present a form for editing a user * @see Rest_Controller::form($resource) */ - public function _form($user, $form_type) { - if ($form_type == "edit") { - $form = user::get_edit_form($user); - print $form; - } else { - return Kohana::show_404(); - } + public function _form_edit($user) { + print user::get_edit_form($user); + } + + /** + * Present a form for adding a user + * @see Rest_Controller::form($resource) + */ + public function _form_add($parameters) { + throw new Exception("@todo User_Controller::_form_add NOT IMPLEMENTED"); } } |