Source

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

Full commit
<?php

class UsersController {
    public function GETAction($parameters) {
        $users = array();
        // imagine retreving data from models

        $users[0] = array('name' => 'Harry Potter',
            'age' => 14,
            'house' => 'Gryffindor'
        );
        $users[1] = array('name' => 'Hermione Granger',
            'age' => 14,
            'house' => 'Gryffindor'
        );
        $users[2] = array('name' => 'Ron Weasley',
            'age' => 14,
            'house' => 'Gryffindor'
        );
        $users[3] = array('name' => 'Ginny Weasley',
            'age' => 13,
            'house' => 'Gryffindor'
        );
        $users[4] = array('name' => 'Cho Chang',
            'age' => 15,
            'house' => 'Ravenclaw'
        );

        $friends[0] = array( // Harry's friends
            'http://api.local/rest/user/1',
            'http://api.local/rest/user/2',
            'http://api.local/rest/user/3',
            'http://api.local/rest/user/4'
        );

        $friends[1] = array( // Hermione's friends 
            'http://api.local/rest/user/0',
            '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;
    }
}