@@ -30,23 +30,34 @@ 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 ()
33
+ public function user_get ($ id = NULL )
34
34
{
35
- if (!$ this ->get ('id ' ))
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
37
+ if ($ id === NULL )
38
+ {
39
+ $ id = $ this ->get ('id ' );
40
+ }
41
+
42
+ // Cast as an int
43
+ $ id = (int ) $ id ;
44
+
45
+ // If not a valid id
46
+ if ($ id <= 0 )
36
47
{
37
48
// Set the response and exit
38
49
$ this ->response (NULL , REST_Controller::HTTP_BAD_REQUEST ); // BAD_REQUEST (400) being the HTTP response code
39
50
}
40
51
41
- // $user = $this->some_model->getSomething($this->get('id') );
52
+ // $user = $this->some_model->getSomething($id );
42
53
$ users = [
43
54
1 => ['id ' => 1 , 'name ' => 'John ' , 'email ' => 'john@example.com ' , 'fact ' => 'Loves coding ' ],
44
55
2 => ['id ' => 2 , 'name ' => 'Jim ' , 'email ' => 'jim@example.com ' , 'fact ' => 'Developed on CodeIgniter ' ],
45
56
3 => ['id ' => 3 , 'name ' => 'Jane ' , 'email ' => 'jane@example.com ' , 'fact ' => 'Lives in the USA ' , ['hobbies ' => ['guitar ' , 'cycling ' ]]],
46
57
];
47
58
48
59
// Get the user from the array, by retrieving the id from the GET request
49
- $ user = isset ($ users [$ this -> get ( ' id ' ) ]) ? $ users [$ this -> get ( ' id ' ) ] : NULL ;
60
+ $ user = isset ($ users [$ id ]) ? $ users [$ id ] : NULL ;
50
61
51
62
if ($ user )
52
63
{
@@ -57,15 +68,15 @@ public function user_get()
57
68
$ this ->set_response ([
58
69
'status ' => FALSE ,
59
70
'error ' => 'User could not be found '
60
- ], REST_Controller::NOT_FOUND ); // NOT_FOUND (404) being the HTTP response code
71
+ ], REST_Controller::HTTP_NOT_FOUND ); // NOT_FOUND (404) being the HTTP response code
61
72
}
62
73
}
63
74
64
75
public function user_post ()
65
76
{
66
- // $this->some_model->update_user($this->get('id') );
77
+ // $this->some_model->update_user( ... );
67
78
$ message = [
68
- 'id ' => $ this -> get ( ' id ' ),
79
+ 'id ' => 100 , // Automatically generated by the model
69
80
'name ' => $ this ->post ('name ' ),
70
81
'email ' => $ this ->post ('email ' ),
71
82
'message ' => 'Added a resource '
@@ -76,9 +87,26 @@ public function user_post()
76
87
77
88
public function user_delete ()
78
89
{
79
- // $this->some_model->delete_something($this->get());
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
92
+ if ($ id === NULL )
93
+ {
94
+ $ id = $ this ->get ('id ' );
95
+ }
96
+
97
+ // Cast as an int
98
+ $ id = (int ) $ id ;
99
+
100
+ // If not a valid id
101
+ if ($ id <= 0 )
102
+ {
103
+ // Set the response and exit
104
+ $ this ->response (NULL , REST_Controller::HTTP_BAD_REQUEST ); // BAD_REQUEST (400) being the HTTP response code
105
+ }
106
+
107
+ // $this->some_model->delete_something($id);
80
108
$ message = [
81
- 'id ' => $ this -> get ( ' id ' ) ,
109
+ 'id ' => $ id ,
82
110
'message ' => 'Deleted the resource '
83
111
];
84
112
@@ -101,9 +129,9 @@ public function users_get()
101
129
else
102
130
{
103
131
$ this ->set_response ([
104
- 'status ' => FALSE ,
105
- 'error ' => 'No users were found '
106
- ], REST_Controller::NOT_FOUND ); // NOT_FOUND (404) being the HTTP response code
132
+ 'status ' => FALSE ,
133
+ 'error ' => 'No users were found '
134
+ ], REST_Controller::HTTP_NOT_FOUND ); // NOT_FOUND (404) being the HTTP response code
107
135
}
108
136
}
109
137
}
0 commit comments