lornajane avatar lornajane committed 86c99c7

adding handling for a JSON PUT request, some refactoring in user controller

Comments (0)

Files changed (3)

 <?php
 
 $data = array(
-    'status' => 'live',
-    'now' => time()
-    );
+    'format' => 'json',
+    'status' => 'live'
+);
 
 echo json_encode($data);

public/rest/controllers.php

 <?php
 
 class UsersController {
-    public function GETAction($parameters) {
+    public function GETAction($path, $data) {
+        // imagine retreving data from models
+        $users = $this->getUsers();
+        $friend = $this->getFriends();
+
+        if(isset($path[2])) {
+            if(isset($path[3]) && $path[3] == 'friends') {
+                return $friends[(int)$path[2]];
+            } else {
+                return $users[(int)$path[2]];
+            }
+        } else {
+            return $users;
+        }
+    }
+
+    public function POSTAction($path, $data) {
+        // sanitise and validate data please!
+
+        // create a user from params
+        $new_user['name'] = isset($data['name']) ? $data['name'] : '';
+        $new_user['house'] = isset($data['house']) ? $data['house'] : '';
+        $new_user['age'] = isset($data['age']) ? $data['age'] : '';
+
+        // save the user, return the new id
+        $new_user['self'] = 'http://' . $_SERVER['HTTP_HOST'] . '/rest/users/5';
+
+        // redirect user
+        header('HTTP/1.1 201 Created');
+        header('Location: http://api.local/rest/users/5');        
+        return $new_user;
+    }
+
+    protected function getUsers() {
         $users = array();
         // imagine retreving data from models
 
         $users[0] = array('name' => 'Harry Potter',
             'age' => 14,
-            'house' => 'Gryffindor'
+            'house' => 'Gryffindor',
+            'self' => urlencode($_SERVER['HTTP_HOST'] . '/rest/users/0')
         );
         $users[1] = array('name' => 'Hermione Granger',
             'age' => 14,
-            'house' => 'Gryffindor'
+            'house' => 'Gryffindor',
+            'self' => $_SERVER['HTTP_HOST'] . '/rest/users/1'
         );
         $users[2] = array('name' => 'Ron Weasley',
             'age' => 14,
-            'house' => 'Gryffindor'
+            'house' => 'Gryffindor',
+            'self' => $_SERVER['HTTP_HOST'] . '/rest/users/2'
         );
         $users[3] = array('name' => 'Ginny Weasley',
             'age' => 13,
-            'house' => 'Gryffindor'
+            'house' => 'Gryffindor',
+            'self' => $_SERVER['HTTP_HOST'] . '/rest/users/3'
         );
         $users[4] = array('name' => 'Cho Chang',
             'age' => 15,
-            'house' => 'Ravenclaw'
+            'house' => 'Ravenclaw',
+            'self' => $_SERVER['HTTP_HOST'] . '/rest/users/4'
         );
+    }
 
+    protected function getFriends() {
         $friends[0] = array( // Harry's friends
             'http://api.local/rest/user/1',
             'http://api.local/rest/user/2',
             'http://api.local/rest/user/3'
         );
 
-            
-
-        if(isset($parameters[2])) {
-            if(isset($parameters[3]) && $parameters[3] == 'friends') {
-                return $friends[(int)$parameters[2]];
-            } else {
-                return $users[(int)$parameters[2]];
-            }
-        } else {
-            return $users;
-        }
-    }
-
-    public function POSTAction() {
-        // sanitise and validate data please!
-        $data = $_POST;
-
-        // create a user from params
-        $user['name'] = $data['name'];
-
-        // save the user, return the new id
-
-        // redirect user
-        header('HTTP/1.1 201 Created');
-        header('Location: http://api.local/rest/users/5');        
-        return $user;
     }
 }

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);
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.