Source

Web Services Tutorial Code / public / rest / index.php

Diff from to

public/rest/index.php

 <?php
 
-// inclue the various view classes
+// include the various view classes
 include('views.php');
 include('controllers.php');
 
     $view = new JsonView();
 }
 
+// grab incoming parameters
+$verb = $_SERVER['REQUEST_METHOD'];
+$data = array();
+switch($verb) {
+    case 'GET':
+        parse_str($_SERVER['QUERY_STRING'], &$data);
+        break;
+    case 'POST':
+    case 'PUT':
+        // incoming JSON data, it's an array
+        $data = json_decode(file_get_contents('php://input'), true);
+        break;
+    case 'DELETE':
+        // do nothing
+        break;
+    default:
+        // WTF?
+        break;
+}
+
 // route the request
-$verb = $_SERVER['REQUEST_METHOD'];
+$path = explode('/',$_SERVER['PATH_INFO']);
 $action_name = strtoupper($verb) . 'Action';
-$url_params = explode('/',$_SERVER['PATH_INFO']);
-$controller_name = ucfirst($url_params[1]) . 'Controller';
+$controller_name = ucfirst($path[1]) . 'Controller';
 $controller = new $controller_name();
-$data = $controller->$action_name($url_params);
+$data = $controller->$action_name($path, $data);
 
 // output appropriately
 $view->render($data);