Skip to content

Commit 4d1a0cc

Browse files
Merge pull request #518 from softwarespot/master
Re-wrote example to be on par with the RESTful approach
2 parents 1c9484a + 7962955 commit 4d1a0cc

File tree

2 files changed

+65
-76
lines changed

2 files changed

+65
-76
lines changed

application/controllers/api/Example.php

+54-41
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,51 @@ function __construct()
3030
$this->methods['user_delete']['limit'] = 50; // 50 requests per hour per user/key
3131
}
3232

33-
public function user_get($id = NULL)
33+
public function users_get($id_param = NULL)
3434
{
35-
// If the id has not been passed via the URL e.g. example/user/:id, then
36-
// check the id query parameter id=? instead
35+
// Users from a data store e.g. database
36+
// $user = $this->some_model->getSomething($id);
37+
$users = [
38+
1 => ['id' => 1, 'name' => 'John', 'email' => '[email protected]', 'fact' => 'Loves coding'],
39+
2 => ['id' => 2, 'name' => 'Jim', 'email' => '[email protected]', 'fact' => 'Developed on CodeIgniter'],
40+
3 => ['id' => 3, 'name' => 'Jane', 'email' => '[email protected]', 'fact' => 'Lives in the USA', ['hobbies' => ['guitar', 'cycling']]],
41+
];
42+
43+
// Get the id parameter value
44+
$id = $this->get('id');
45+
46+
// If NULL, then check the id passed as users/:id
47+
if ($id === NULL)
48+
{
49+
$id = $id_param;
50+
}
51+
52+
// If the id parameter and query parameter don't exist, return all users instead
3753
if ($id === NULL)
3854
{
39-
$id = $this->get('id');
55+
// Check if the users data store contains users (in case the database result returns NULL)
56+
if ($users)
57+
{
58+
// Set the response and exit
59+
$this->response($users, REST_Controller::HTTP_OK); // OK (200) being the HTTP response code
60+
}
61+
else
62+
{
63+
// Set the response and exit
64+
$this->response([
65+
'status' => FALSE,
66+
'error' => 'No users were found'
67+
], REST_Controller::HTTP_NOT_FOUND); // NOT_FOUND (404) being the HTTP response code
68+
}
69+
4070
}
4171

42-
// Cast as an int
43-
$id = (int) $id;
72+
// Check if the id is a valid integer
73+
if (ctype_digit($id))
74+
{
75+
// Cast as an int
76+
$id = (int) $id;
77+
}
4478

4579
// If not a valid id
4680
if ($id <= 0)
@@ -49,16 +83,10 @@ public function user_get($id = NULL)
4983
$this->response(NULL, REST_Controller::HTTP_BAD_REQUEST); // BAD_REQUEST (400) being the HTTP response code
5084
}
5185

52-
// $user = $this->some_model->getSomething($id);
53-
$users = [
54-
1 => ['id' => 1, 'name' => 'John', 'email' => '[email protected]', 'fact' => 'Loves coding'],
55-
2 => ['id' => 2, 'name' => 'Jim', 'email' => '[email protected]', 'fact' => 'Developed on CodeIgniter'],
56-
3 => ['id' => 3, 'name' => 'Jane', 'email' => '[email protected]', 'fact' => 'Lives in the USA', ['hobbies' => ['guitar', 'cycling']]],
57-
];
58-
5986
// Get the user from the array, by retrieving the id from the GET request
6087
$user = isset($users[$id]) ? $users[$id] : NULL;
6188

89+
// If a user exists in the data store e.g. database
6290
if ($user)
6391
{
6492
$this->set_response($user, REST_Controller::HTTP_OK); // OK (200) being the HTTP response code
@@ -72,7 +100,7 @@ public function user_get($id = NULL)
72100
}
73101
}
74102

75-
public function user_post()
103+
public function users_post()
76104
{
77105
// $this->some_model->update_user( ... );
78106
$message = [
@@ -85,17 +113,23 @@ public function user_post()
85113
$this->set_response($message, REST_Controller::HTTP_CREATED); // CREATED (201) being the HTTP response code
86114
}
87115

88-
public function user_delete()
116+
public function users_delete($id_param = NULL)
89117
{
90-
// If the id has not been passed via the URL e.g. example/user/:id, then
91-
// check the id query parameter id=? instead
118+
// Get the id parameter value
119+
$id = $this->get('id');
120+
121+
// If NULL, then check the id passed as users/:id
92122
if ($id === NULL)
93123
{
94-
$id = $this->get('id');
124+
$id = $id_param;
95125
}
96126

97-
// Cast as an int
98-
$id = (int) $id;
127+
// Check if the id is a valid integer
128+
if (ctype_digit($id))
129+
{
130+
// Cast as an int
131+
$id = (int) $id;
132+
}
99133

100134
// If not a valid id
101135
if ($id <= 0)
@@ -113,25 +147,4 @@ public function user_delete()
113147
$this->set_response($message, REST_Controller::HTTP_NO_CONTENT); // NO_CONTENT (204) being the HTTP response code
114148
}
115149

116-
public function users_get()
117-
{
118-
// $users = $this->some_model->get_something($this->get('limit'));
119-
$users = [
120-
['id' => 1, 'name' => 'John', 'email' => '[email protected]', 'fact' => 'Loves coding'],
121-
['id' => 2, 'name' => 'Jim', 'email' => '[email protected]', 'fact' => 'Developed on CodeIgniter'],
122-
3 => ['id' => 3, 'name' => 'Jane', 'email' => '[email protected]', 'fact' => 'Lives in the USA', ['hobbies' => ['guitar', 'cycling']]],
123-
];
124-
125-
if ($users)
126-
{
127-
$this->set_response($users, REST_Controller::HTTP_OK); // OK (200) being the HTTP response code
128-
}
129-
else
130-
{
131-
$this->set_response([
132-
'status' => FALSE,
133-
'error' => 'No users were found'
134-
], REST_Controller::HTTP_NOT_FOUND); // NOT_FOUND (404) being the HTTP response code
135-
}
136-
}
137150
}

application/controllers/api/Key.php

+11-35
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,10 @@ class Key extends REST_Controller {
2626
];
2727

2828
/**
29-
* Key Create
3029
* Insert a key into the database
3130
*
32-
* @access public
33-
* @return void
31+
* @access public
32+
* @return void
3433
*/
3534
public function index_put()
3635
{
@@ -58,14 +57,11 @@ public function index_put()
5857
}
5958
}
6059

61-
// --------------------------------------------------------------------
62-
6360
/**
64-
* Key Delete
6561
* Remove a key from the database to stop it working
6662
*
67-
* @access public
68-
* @return void
63+
* @access public
64+
* @return void
6965
*/
7066
public function index_delete()
7167
{
@@ -91,14 +87,11 @@ public function index_delete()
9187
], REST_Controller::HTTP_NO_CONTENT); // NO_CONTENT (204) being the HTTP response code
9288
}
9389

94-
// --------------------------------------------------------------------
95-
9690
/**
97-
* Update Key
9891
* Change the level
9992
*
100-
* @access public
101-
* @return void
93+
* @access public
94+
* @return void
10295
*/
10396
public function level_post()
10497
{
@@ -132,14 +125,11 @@ public function level_post()
132125
}
133126
}
134127

135-
// --------------------------------------------------------------------
136-
137128
/**
138-
* Update Key
139129
* Change the level
140130
*
141-
* @access public
142-
* @return void
131+
* @access public
132+
* @return void
143133
*/
144134
public function suspend_post()
145135
{
@@ -172,14 +162,11 @@ public function suspend_post()
172162
}
173163
}
174164

175-
// --------------------------------------------------------------------
176-
177165
/**
178-
* Regenerate Key
179166
* Remove a key from the database to stop it working
180167
*
181-
* @access public
182-
* @return void
168+
* @access public
169+
* @return void
183170
*/
184171
public function regenerate_post()
185172
{
@@ -219,8 +206,6 @@ public function regenerate_post()
219206
}
220207
}
221208

222-
// --------------------------------------------------------------------
223-
224209
/* Helper Methods */
225210

226211
private function _generate_key()
@@ -243,8 +228,6 @@ private function _generate_key()
243228
return $new_key;
244229
}
245230

246-
// --------------------------------------------------------------------
247-
248231
/* Private Data Methods */
249232

250233
private function _get_key($key)
@@ -255,17 +238,13 @@ private function _get_key($key)
255238
->row();
256239
}
257240

258-
// --------------------------------------------------------------------
259-
260241
private function _key_exists($key)
261242
{
262243
return $this->db
263244
->where(config_item('rest_key_column'), $key)
264245
->count_all_results(config_item('rest_keys_table')) > 0;
265246
}
266247

267-
// --------------------------------------------------------------------
268-
269248
private function _insert_key($key, $data)
270249
{
271250
$data[config_item('rest_key_column')] = $key;
@@ -276,21 +255,18 @@ private function _insert_key($key, $data)
276255
->insert(config_item('rest_keys_table'));
277256
}
278257

279-
// --------------------------------------------------------------------
280-
281258
private function _update_key($key, $data)
282259
{
283260
return $this->db
284261
->where(config_item('rest_key_column'), $key)
285262
->update(config_item('rest_keys_table'), $data);
286263
}
287264

288-
// --------------------------------------------------------------------
289-
290265
private function _delete_key($key)
291266
{
292267
return $this->db
293268
->where(config_item('rest_key_column'), $key)
294269
->delete(config_item('rest_keys_table'));
295270
}
271+
296272
}

0 commit comments

Comments
 (0)