@@ -30,17 +30,51 @@ function __construct()
30
30
$ this ->methods ['user_delete ' ]['limit ' ] = 50 ; // 50 requests per hour per user/key
31
31
}
32
32
33
- public function user_get ( $ id = NULL )
33
+ public function users_get ( $ id_param = NULL )
34
34
{
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
37
53
if ($ id === NULL )
38
54
{
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
+
40
70
}
41
71
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
+ }
44
78
45
79
// If not a valid id
46
80
if ($ id <= 0 )
@@ -49,16 +83,10 @@ public function user_get($id = NULL)
49
83
$ this ->response (NULL , REST_Controller::HTTP_BAD_REQUEST ); // BAD_REQUEST (400) being the HTTP response code
50
84
}
51
85
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
-
59
86
// Get the user from the array, by retrieving the id from the GET request
60
87
$ user = isset ($ users [$ id ]) ? $ users [$ id ] : NULL ;
61
88
89
+ // If a user exists in the data store e.g. database
62
90
if ($ user )
63
91
{
64
92
$ 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)
72
100
}
73
101
}
74
102
75
- public function user_post ()
103
+ public function users_post ()
76
104
{
77
105
// $this->some_model->update_user( ... );
78
106
$ message = [
@@ -85,17 +113,23 @@ public function user_post()
85
113
$ this ->set_response ($ message , REST_Controller::HTTP_CREATED ); // CREATED (201) being the HTTP response code
86
114
}
87
115
88
- public function user_delete ( )
116
+ public function users_delete ( $ id_param = NULL )
89
117
{
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
92
122
if ($ id === NULL )
93
123
{
94
- $ id = $ this -> get ( ' id ' ) ;
124
+ $ id = $ id_param ;
95
125
}
96
126
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
+ }
99
133
100
134
// If not a valid id
101
135
if ($ id <= 0 )
@@ -113,25 +147,4 @@ public function user_delete()
113
147
$ this ->set_response ($ message , REST_Controller::HTTP_NO_CONTENT ); // NO_CONTENT (204) being the HTTP response code
114
148
}
115
149
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
- }
137
150
}
0 commit comments