@@ -30,7 +30,7 @@ function __construct()
30
30
$ this ->methods ['user_delete ' ]['limit ' ] = 50 ; // 50 requests per hour per user/key
31
31
}
32
32
33
- public function users_get ($ id = NULL )
33
+ public function users_get ($ id_param = NULL )
34
34
{
35
35
// Users from a data store e.g. database
36
36
// $user = $this->some_model->getSomething($id);
@@ -40,8 +40,17 @@ public function users_get($id = NULL)
40
40
3 => ['id ' => 3 , 'name ' => 'Jane ' , 'email ' => 'jane@example.com ' , 'fact ' => 'Lives in the USA ' , ['hobbies ' => ['guitar ' , 'cycling ' ]]],
41
41
];
42
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
+
43
52
// If the id parameter and query parameter don't exist, return all users instead
44
- if ($ id === NULL && $ this -> get ( ' id ' ) === NULL )
53
+ if ($ id === NULL )
45
54
{
46
55
// Check if the users data store contains users (in case the database result returns NULL)
47
56
if ($ users )
@@ -60,16 +69,13 @@ public function users_get($id = NULL)
60
69
61
70
}
62
71
63
- // If the id has not been passed via the URL e.g. example/users/:id, then
64
- // check the id query parameter id=? instead
65
- if ($ id === NULL || ctype_digit ($ id ) === FALSE )
72
+ // Check if the id is a valid integer
73
+ if (ctype_digit ($ id ))
66
74
{
67
- $ id = $ this ->get ('id ' );
75
+ // Cast as an int
76
+ $ id = (int ) $ id ;
68
77
}
69
78
70
- // Cast as an int
71
- $ id = (int ) $ id ;
72
-
73
79
// If not a valid id
74
80
if ($ id <= 0 )
75
81
{
@@ -107,17 +113,23 @@ public function users_post()
107
113
$ this ->set_response ($ message , REST_Controller::HTTP_CREATED ); // CREATED (201) being the HTTP response code
108
114
}
109
115
110
- public function users_delete ()
116
+ public function users_delete ($ id_param = NULL )
111
117
{
112
- // If the id has not been passed via the URL e.g. example/users/:id, then
113
- // 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
114
122
if ($ id === NULL )
115
123
{
116
- $ id = $ this -> get ( ' id ' ) ;
124
+ $ id = $ id_param ;
117
125
}
118
126
119
- // Cast as an int
120
- $ 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
+ }
121
133
122
134
// If not a valid id
123
135
if ($ id <= 0 )
0 commit comments