Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 157 lines (137 sloc) 4.607 kb
81db6f9 initial import
Federico authored
1 <?php
2 class UsersController extends Controller
3 {
4 /**
5 * @route GET /?method=users
6 * @route GET /users
7 *
8 * @param Request $request
5a131ad @fedecarg Added pagenotfound script and updated examples
authored
9 * @return Response|View
81db6f9 initial import
Federico authored
10 */
11 public function indexAction($request)
12 {
82c7002 updated examples
Federico authored
13 // serve HTML, JSON and XML
14 $request->acceptContentTypes(array('html', 'json', 'xml'));
81db6f9 initial import
Federico authored
15
82c7002 updated examples
Federico authored
16 if ('html' == $request->getContentType()) {
17 $response = new View();
18 $response->setLayout('main');
19 } else {
20 $response = new Response();
21 }
22
23 $response->users = $this->getModel('User')->findAll();
81db6f9 initial import
Federico authored
24 return $response;
25 }
26
27 /**
28 * @route GET /?method=users.show&id=1
bfe4c85 initial import
Federico authored
29 * @route GET /?method=users.show&id=matt
29a7443 updated examples
Federico authored
30 * @route GET /users/1
31 * @route GET /users/matt
81db6f9 initial import
Federico authored
32 *
33 * @param Request $request
5a131ad @fedecarg Added pagenotfound script and updated examples
authored
34 * @return Response|View
35 * @throws Exception
81db6f9 initial import
Federico authored
36 */
37 public function showAction($request)
38 {
82c7002 updated examples
Federico authored
39 // serve HTML, JSON and XML
40 $request->acceptContentTypes(array('html', 'json', 'xml'));
41
81db6f9 initial import
Federico authored
42 $model = $this->getModel('User');
43 $id = $request->getParam('id');
44 $user = is_numeric($id) ? $model->find($id) : $model->findBy(array('username'=>$id));
45 if (! $user) {
3d0ee55 changed return to throw
Federico authored
46 throw new Exception('User not found', Response::NOT_FOUND);
81db6f9 initial import
Federico authored
47 }
48
82c7002 updated examples
Federico authored
49 if ('html' == $request->getContentType()) {
50 $response = new View();
51 $response->setLayout('main');
52 } else {
53 $response = new Response();
54 $response->setEtagHeader(md5('/users/' . $user->id));
55 }
321ed2d updated examples
Federico authored
56
82c7002 updated examples
Federico authored
57 $response->user = $user;
81db6f9 initial import
Federico authored
58 return $response;
59 }
60
61 /**
82c7002 updated examples
Federico authored
62 * @route POST /?method=users.create&format=json
63 * @route POST /users/create.json
81db6f9 initial import
Federico authored
64 *
65 * @param Request $request
5a131ad @fedecarg Added pagenotfound script and updated examples
authored
66 * @return Response
67 * @throws Exception
81db6f9 initial import
Federico authored
68 */
69 public function createAction($request)
70 {
82c7002 updated examples
Federico authored
71 $request->acceptContentTypes(array('json'));
29a7443 updated examples
Federico authored
72 if ('POST' != $request->getMethod()) {
3d0ee55 changed return to throw
Federico authored
73 throw new Exception('HTTP method not allowed', Response::NOT_ALLOWED);
81db6f9 initial import
Federico authored
74 }
75
2a37e5d updated examples
Federico authored
76 try {
77 $user = new User(array(
78 'name' => $request->getPost('name'),
79 'username' => $request->getPost('username'),
80 'email' => $request->getPost('email'),
81 'gender' => $request->getPost('gender')
82 ));
83 } catch (ValidationException $e) {
3d0ee55 changed return to throw
Federico authored
84 throw new Exception($e->getMessage(), Response::OK);
2a37e5d updated examples
Federico authored
85 }
81db6f9 initial import
Federico authored
86
29a7443 updated examples
Federico authored
87 $id = $this->getModel('User')->save($user);
81db6f9 initial import
Federico authored
88 if (! is_numeric($id)) {
3d0ee55 changed return to throw
Federico authored
89 throw new Exception('An error occurred while creating user', Response::OK);
81db6f9 initial import
Federico authored
90 }
91
2b6b359 added setDefaultContentType, acceptContentTypes methods and updated e…
Federico authored
92 $response = new Response();
bfe4c85 initial import
Federico authored
93 $response->setCode(Response::CREATED);
94 $response->setEtagHeader(md5('/users/' . $id));
95
81db6f9 initial import
Federico authored
96 return $response;
97 }
98
99 /**
82c7002 updated examples
Federico authored
100 * @route POST /?method=users.update&id=1&format=json
101 * @route POST /users/1/update.json
81db6f9 initial import
Federico authored
102 *
103 * @param Request $request
5a131ad @fedecarg Added pagenotfound script and updated examples
authored
104 * @return Response
105 * @throws Exception
81db6f9 initial import
Federico authored
106 */
107 public function updateAction($request)
321ed2d updated examples
Federico authored
108 {
82c7002 updated examples
Federico authored
109 $request->acceptContentTypes(array('json'));
29a7443 updated examples
Federico authored
110 if ('POST' != $request->getMethod()) {
3d0ee55 changed return to throw
Federico authored
111 throw new Exception('HTTP method not supported', Response::NOT_ALLOWED);
82c7002 updated examples
Federico authored
112 }
81db6f9 initial import
Federico authored
113
114 $id = $request->getParam('id');
29a7443 updated examples
Federico authored
115
116 $model = $this->getModel('User');
81db6f9 initial import
Federico authored
117 $user = $model->find($id);
118 if (! $user) {
3d0ee55 changed return to throw
Federico authored
119 throw new Exception('User not found', Response::NOT_FOUND);
81db6f9 initial import
Federico authored
120 }
121
321ed2d updated examples
Federico authored
122 try {
123 $user->username = $request->getPost('username');
2a37e5d updated examples
Federico authored
124 } catch (ValidationException $e) {
3d0ee55 changed return to throw
Federico authored
125 throw new Exception($e->getMessage(), Response::OK);
321ed2d updated examples
Federico authored
126 }
81db6f9 initial import
Federico authored
127 $model->save($user);
128
321ed2d updated examples
Federico authored
129 // return 200 OK
2b6b359 added setDefaultContentType, acceptContentTypes methods and updated e…
Federico authored
130 return new Response();
81db6f9 initial import
Federico authored
131 }
132
133 /**
82c7002 updated examples
Federico authored
134 * @route GET /?method=users.destroy&id=1&format=json
135 * @route GET /users/1/destroy.json
81db6f9 initial import
Federico authored
136 *
137 * @param Request $request
5a131ad @fedecarg Added pagenotfound script and updated examples
authored
138 * @return Response
139 * @throws Exception
81db6f9 initial import
Federico authored
140 */
141 public function destroyAction($request)
142 {
82c7002 updated examples
Federico authored
143 $request->acceptContentTypes(array('json'));
29a7443 updated examples
Federico authored
144
82c7002 updated examples
Federico authored
145 $id = $request->getParam('id');
29a7443 updated examples
Federico authored
146 $model = $this->getModel('User');
81db6f9 initial import
Federico authored
147 $user = $model->find($id);
148 if (! $user) {
3d0ee55 changed return to throw
Federico authored
149 throw new Exception('User not found', Response::NOT_FOUND);
81db6f9 initial import
Federico authored
150 }
151 $model->delete($user->id);
152
321ed2d updated examples
Federico authored
153 // return 200 OK
2b6b359 added setDefaultContentType, acceptContentTypes methods and updated e…
Federico authored
154 return new Response();
81db6f9 initial import
Federico authored
155 }
156 }
Something went wrong with that request. Please try again.