@@ -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}
0 commit comments