1: <?php defined('BASEPATH') OR exit('No direct script access allowed');
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
15:
16:
17: require APPPATH.'/libraries/REST_Controller.php';
18:
19: class Example extends REST_Controller
20: {
21: function __construct()
22: {
23:
24: parent::__construct();
25:
26:
27:
28:
29: $this->methods['user_get']['limit'] = 500;
30: $this->methods['user_post']['limit'] = 100;
31: $this->methods['user_delete']['limit'] = 50;
32: }
33:
34: function user_get()
35: {
36: if(!$this->get('id'))
37: {
38: $this->response(NULL, 400);
39: }
40:
41:
42: $users = array(
43: 1 => array('id' => 1, 'name' => 'Some Guy', 'email' => 'example1@example.com', 'fact' => 'Loves swimming'),
44: 2 => array('id' => 2, 'name' => 'Person Face', 'email' => 'example2@example.com', 'fact' => 'Has a huge face'),
45: 3 => array('id' => 3, 'name' => 'Scotty', 'email' => 'example3@example.com', 'fact' => 'Is a Scott!', array('hobbies' => array('fartings', 'bikes'))),
46: );
47:
48: $user = @$users[$this->get('id')];
49:
50: if($user)
51: {
52: $this->response($user, 200);
53: }
54:
55: else
56: {
57: $this->response(array('error' => 'User could not be found'), 404);
58: }
59: }
60:
61: function user_post()
62: {
63:
64: $message = array('id' => $this->get('id'), 'name' => $this->post('name'), 'email' => $this->post('email'), 'message' => 'ADDED!');
65:
66: $this->response($message, 200);
67: }
68:
69: function user_delete()
70: {
71:
72: $message = array('id' => $this->get('id'), 'message' => 'DELETED!');
73:
74: $this->response($message, 200);
75: }
76:
77: function users_get()
78: {
79:
80: $users = array(
81: array('id' => 1, 'name' => 'Some Guy', 'email' => 'example1@example.com'),
82: array('id' => 2, 'name' => 'Person Face', 'email' => 'example2@example.com'),
83: 3 => array('id' => 3, 'name' => 'Scotty', 'email' => 'example3@example.com', 'fact' => array('hobbies' => array('fartings', 'bikes'))),
84: );
85:
86: if($users)
87: {
88: $this->response($users, 200);
89: }
90:
91: else
92: {
93: $this->response(array('error' => 'Couldn\'t find any users!'), 404);
94: }
95: }
96:
97:
98: public function send_post()
99: {
100: var_dump($this->request->body);
101: }
102:
103:
104: public function send_put()
105: {
106: var_dump($this->put('foo'));
107: }
108: }