-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclean_path.php
More file actions
43 lines (39 loc) · 1.01 KB
/
clean_path.php
File metadata and controls
43 lines (39 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php
class books {
static public function get($request) {
echo "Get Books";
}
static public function post($request) {
echo "Create a Book";
}
}
class computers {
static public function get($request) {
echo "Get Computers";
}
}
$request = explode('/', $_GET['PATH_INFO']);
array_shift($request);
$resource = array_shift($request);
// only process valid resources
$resources = array('books' => true, 'computers' => true);
if (!array_key_exists($resource, $resources)) {
http_response_code(404);
exit;
}
// route the request to the appropriate function based on method
$method = strtolower($_SERVER["REQUEST_METHOD"]);
switch ($method) {
case 'get':
case 'post':
case 'put':
case 'delete':
// any other methods you want to support, such as HEAD
if (method_exists($resource, $method)) {
call_user_func(array($resource, $method), $request);
break;
}
// fall through
default:
http_response_code(405);
}