Skip to content

Commit 1e74e81

Browse files
committed
Release version 1.1.0
- Request supports body params - Optimize Controller with supporting routes
1 parent bf46cdd commit 1e74e81

File tree

6 files changed

+163
-77
lines changed

6 files changed

+163
-77
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2017 Nick Tsai
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,16 @@ public function update($resourceID, $requestData=null) {
5858

5959
$this->db->where('id', $resourceID)
6060
->update('table', $requestData);
61-
return $this->json(null, true);
61+
return $this->json(false, true);
6262
}
6363
```
6464

65+
Output:
66+
67+
```json
68+
{"code":200}
69+
```
70+
6571
---
6672

6773
REQUIREMENTS
@@ -122,20 +128,21 @@ The base RESTful API controller is `yidas\rest\Controller`, the following table
122128
|GET |/photos/{photo}|show |Retrieve an addressed member of the collection.|
123129
|PUT/PATCH |/photos/{photo}|update |Update the addressed member of the collection. |
124130
|DELETE |/photos/{photo}|delete |Delete the addressed member of the collection. |
125-
|DELETE |/photos |deleteAll|Delete the entire collection. |
131+
|DELETE |/photos |delete |Delete the entire collection. |
126132

127133

128134
### Overrided Methods:
129135

136+
You could make a resource controller by referring the [Template of Resource Controller](https://github.com/yidas/codeigniter-rest/blob/dev/examples/RestController.php).
137+
130138
The following methods with arguments could be overrided when you need to defind response and open it:
131139

132140
```php
133141
public function index() {}
134-
public function store($requestData=null) {}
135-
public function show($resourceID) {}
136-
public function update($resourceID, $requestData=null) {}
137-
public function delete($resourceID) {}
138-
public function deleteAll() {}
142+
protected function store($requestData=null) {}
143+
protected function show($resourceID) {}
144+
protected function update($resourceID, $requestData=null) {}
145+
protected function delete($resourceID=null) {}
139146
```
140147

141148
> `$requestData` is the raw body from request

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"keywords": ["codeIgniter", "api", "controller", "authorization"],
55
"homepage": "https://github.com/yidas/codeigniter-api-controller",
66
"type": "library",
7-
"license": "BSD-3-Clause",
7+
"license": "MIT",
88
"support": {
99
"issues": "https://github.com/yidas/codeigniter-api-controller/issues",
1010
"source": "https://github.com/yidas/codeigniter-api-controller"

examples/RestController.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
class RestController extends \yidas\rest\Controller
4+
{
5+
/**
6+
* Action: Index
7+
*
8+
* This method could be `public` property for none-routes usage
9+
*/
10+
public function index() {}
11+
12+
/**
13+
* Action: Store
14+
*
15+
* @param array $requestData
16+
*/
17+
protected function store($requestData=null) {}
18+
19+
/**
20+
* Action: Show
21+
*
22+
* @param int|string $resourceID
23+
*/
24+
protected function show($resourceID) {}
25+
26+
/**
27+
* Action: Update
28+
*
29+
* @param int|string $resourceID
30+
* @param array $requestData
31+
*/
32+
protected function update($resourceID, $requestData=null) {}
33+
34+
/**
35+
* Action: Delete
36+
*
37+
* @param int|string $resourceID
38+
*/
39+
protected function delete($resourceID) {}
40+
}

src/http/Request.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Request Component
77
*
88
* @author Nick Tsai <myintaer@gmail.com>
9-
* @since 0.1.0
9+
* @since 1.1.0
1010
* @todo Psr\Http\Message\RequestInterface
1111
*/
1212
class Request
@@ -44,12 +44,29 @@ public function getRawBody()
4444
return $this->_rawBody;
4545
}
4646

47+
/**
48+
* Returns the request parameters given in the request body.
49+
*
50+
* Request parameters are determined using the parsers configured in [[parsers]] property.
51+
* If no parsers are configured for the current [[contentType]] it uses the PHP function `mb_parse_str()`
52+
* to parse the [[rawBody|request body]].
53+
*
54+
* @todo Cache
55+
* @return array the request parameters given in the request body.
56+
*/
57+
public function getBodyParams()
58+
{
59+
mb_parse_str($this->getRawBody(), $bodyParams);
60+
61+
return $bodyParams;
62+
}
63+
4764
/**
4865
* Alias of getRawBody()
4966
*/
5067
public function input()
5168
{
52-
return $this->getRawBody();
69+
return $this->getBodyParams();
5370
}
5471

5572
/**

src/rest/Controller.php

Lines changed: 68 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
* RESTful API Controller
1010
*
1111
* @author Nick Tsai <myintaer@gmail.com>
12-
* @version 1.0.1
12+
* @version 1.1.0
13+
* @link https://github.com/yidas/codeigniter-rest/
14+
* @see https://github.com/yidas/codeigniter-rest/blob/master/examples/RestController.php
1315
*
1416
* Controller extending:
1517
* ```php
@@ -24,11 +26,6 @@
2426
*/
2527
class Controller extends \CI_Controller
2628
{
27-
/**
28-
* @var bool Close the default actions without implement
29-
*/
30-
const CLOSE_DEFAULT_ACTIONS = true;
31-
3229
/**
3330
* @var array Standard format
3431
*/
@@ -39,10 +36,38 @@ class Controller extends \CI_Controller
3936
];
4037

4138
/**
39+
* RESTful API resource routes
40+
*
41+
* public function index() {}
42+
* protected function store($requestData=null) {}
43+
* protected function show($resourceID) {}
44+
* protected function update($resourceID, $requestData=null) {}
45+
* protected function delete($resourceID=null) {}
46+
*
47+
* @var array RESTful API table of routes & actions
48+
*/
49+
protected $routes = [
50+
'index' => 'index',
51+
'store' => 'store',
52+
'show' => 'show',
53+
'update' => 'update',
54+
'delete' => 'delete',
55+
];
56+
57+
/**
58+
* Pre-setting format
59+
*
4260
* @var string yidas\http\Response format
4361
*/
4462
protected $format;
4563

64+
/**
65+
* Body Format usage switch
66+
*
67+
* @var bool Default $bodyFormat for json()
68+
*/
69+
protected $bodyFormat = false;
70+
4671
/**
4772
* @var object yidas\http\Request;
4873
*/
@@ -81,33 +106,41 @@ public function route($resourceID=NULL)
81106
switch ($this->request->getMethod()) {
82107
case 'POST':
83108
if (!$resourceID) {
84-
$this->store($this->request->getRawBody());
109+
return $this->_action(['delete', $this->request->getBodyParams()]);
85110
}
86111
break;
87112
case 'PUT':
88113
case 'PATCH':
89114
if ($resourceID) {
90-
$this->update($this->request->getRawBody(), $resourceID);
115+
return $this->_action(['update', $resourceID, $this->request->getBodyParams()]);
91116
}
92117
break;
93118
case 'DELETE':
94119
if ($resourceID) {
95-
$this->delete($resourceID);
120+
return $this->_action(['delete', $resourceID]);
96121
} else {
97-
$this->deleteAll();
122+
return $this->_action(['delete']);
98123
}
99124
break;
100125
case 'GET':
101126
default:
102127
if ($resourceID) {
103-
$this->show($resourceID);
128+
return $this->_action(['show', $resourceID]);
104129
} else {
105-
$this->index();
130+
return $this->_action(['index']);
106131
}
107132
break;
108133
}
109134
}
110135

136+
/**
137+
* Alias of route()
138+
*/
139+
public function ajax($resourceID=NULL)
140+
{
141+
return $this->route($resourceID);
142+
}
143+
111144
/**
112145
* Output by JSON format with optinal body format
113146
*
@@ -117,13 +150,16 @@ public function route($resourceID=NULL)
117150
* @param string Callback status text
118151
* @return string Response body data
119152
*/
120-
protected function json($data, $bodyFormat=false, $statusCode=null, $statusText=null)
153+
protected function json($data, $bodyFormat=null, $statusCode=null, $statusText=null)
121154
{
122155
$this->response->setFormat(Response::FORMAT_JSON);
156+
157+
// Check default Body Format setting if not assigning
158+
$bodyFormat = ($bodyFormat!==null) ? $bodyFormat : $this->bodyFormat;
123159

124160
if ($bodyFormat) {
125161
// Pack data
126-
$data = $this->format($statusCode, $statusText, $data);
162+
$data = $this->_format($statusCode, $statusText, $data);
127163
}
128164

129165
return $this->response
@@ -139,7 +175,7 @@ protected function json($data, $bodyFormat=false, $statusCode=null, $statusText=
139175
* @param array|mixed|bool Callback data body, false will remove body key
140176
* @return array Formated array data
141177
*/
142-
protected function format($statusCode=null, $statusText=null, $body=false)
178+
protected function _format($statusCode=null, $statusText=null, $body=false)
143179
{
144180
$format = [];
145181
// Status Code
@@ -160,72 +196,37 @@ protected function format($statusCode=null, $statusText=null, $body=false)
160196
/**
161197
* Default Action
162198
*/
163-
private function _defaultAction()
199+
protected function _defaultAction()
164200
{
165201
/* Response sample code */
166202
// $response->data = ['foo'=>'bar'];
167203
// $response->setStatusCode(401);
168204

169-
if (static::CLOSE_DEFAULT_ACTIONS) {
170-
// Codeigniter 404 Error Handling
171-
show_404();
172-
}
205+
// Codeigniter 404 Error Handling
206+
show_404();
173207
}
174208

175209
/**
176-
* Action: Index
177-
*/
178-
public function index()
179-
{
180-
$this->_defaultAction();
181-
}
182-
183-
/**
184-
* Action: Store
210+
* Action processor for route
185211
*
186-
* @param array $requestData
212+
* @param array Elements contains method for first and params for others
187213
*/
188-
public function store($requestData=null)
214+
private function _action($params)
189215
{
190-
$this->_defaultAction();
191-
}
216+
// Shift and get the method
217+
$method = array_shift($params);
192218

193-
/**
194-
* Action: Show
195-
*
196-
* @param int|string $resourceID
197-
*/
198-
public function show($resourceID)
199-
{
200-
$this->_defaultAction();
201-
}
219+
if (!isset($this->routes[$method])) {
220+
$this->_defaultAction();
221+
}
202222

203-
/**
204-
* Action: Update
205-
*
206-
* @param int|string $resourceID
207-
* @param array $requestData
208-
*/
209-
public function update($resourceID, $requestData=null)
210-
{
211-
$this->_defaultAction();
212-
}
223+
// Get corresponding method name
224+
$method = $this->routes[$method];
213225

214-
/**
215-
* Action: Delete
216-
*
217-
* @param int|string $resourceID
218-
*/
219-
public function delete($resourceID)
220-
{
221-
$this->_defaultAction();
222-
}
226+
if (!method_exists($this, $method)) {
227+
$this->_defaultAction();
228+
}
223229

224-
/**
225-
* Action: Delete All
226-
*/
227-
public function deleteAll()
228-
{
229-
$this->_defaultAction();
230+
return call_user_func_array([$this, $method], $params);
230231
}
231232
}

0 commit comments

Comments
 (0)