Skip to content

Commit

Permalink
Support full REST relationship
Browse files Browse the repository at this point in the history
  • Loading branch information
yidas committed Aug 23, 2018
1 parent e198cd8 commit 213bf12
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,8 @@ The following RESTful controller methods could be add by your need. which each m
public function index() {}
protected function store($requestData=null) {}
protected function show($resourceID) {}
protected function update($resourceID, $requestData=null) {}
protected function delete($resourceID=null) {}
protected function update($resourceID=null, $requestData=null) {}
protected function delete($resourceID=null, $requestData=null) {}
```

> `$requestData` (array) is the raw body from request
Expand Down Expand Up @@ -371,3 +371,5 @@ REFERENCE

- [RFC6750 - The OAuth 2.0 Authorization Framework: Bearer Token Usage](https://tools.ietf.org/html/rfc6750)

- [REST Relationship between URL and HTTP methods](https://en.wikipedia.org/wiki/Representational_state_transfer#Relationship_between_URL_and_HTTP_methods)

7 changes: 4 additions & 3 deletions examples/RestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@ protected function show($resourceID) {}
* @param int|string $resourceID
* @param array $requestData
*/
protected function update($resourceID, $requestData=null) {}
protected function update($resourceID=null, $requestData=null) {}

/**
* Action: Delete
*
* @param int|string $resourceID
* @param int|string $resourceID Support single resource delete
* @param array $requestData Support delete parameters
*/
protected function delete($resourceID) {}
protected function delete($resourceID=null, $requestData=null) {}
}
15 changes: 7 additions & 8 deletions src/rest/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* @version 1.4.0
* @link https://github.com/yidas/codeigniter-rest/
* @see https://github.com/yidas/codeigniter-rest/blob/master/examples/RestController.php
* @see https://en.wikipedia.org/wiki/Representational_state_transfer#Relationship_between_URL_and_HTTP_methods
*
* Controller extending:
* ```php
Expand Down Expand Up @@ -103,18 +104,16 @@ public function route($resourceID=NULL)
return $this->_action(['store', $this->request->getBodyParams()]);
}
break;
case 'PUT':
case 'PATCH':
if ($resourceID) {
return $this->_action(['update', $resourceID, $this->request->getBodyParams()]);
// PATCH could only allow single element
if (!$resourceID) {
return $this->_defaultAction();
}
case 'PUT':
return $this->_action(['update', $resourceID, $this->request->getBodyParams()]);
break;
case 'DELETE':
if ($resourceID) {
return $this->_action(['delete', $resourceID]);
} else {
return $this->_action(['delete']);
}
return $this->_action(['delete', $resourceID, $this->request->getBodyParams()]);
break;
case 'GET':
default:
Expand Down

0 comments on commit 213bf12

Please sign in to comment.