-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.php
80 lines (66 loc) · 1.86 KB
/
routes.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
/**
* Local variables
* @var \Phalcon\JsonApi\Application $app
*/
use Phalcon\JsonApi\Controller\MainController;
use Phalcon\JsonApi\Model\Author;
use Phalcon\JsonApi\Model\Comment;
use Phalcon\JsonApi\Model\Post;
use Phalcon\JsonApi\Model\Site;
//-----------------------------------------------
// Begin Routing
//-----------------------------------------------
$app->get('/', function () use ($app) {
return ["Hello, world"];
});
$app->resource('main', MainController::class);
$app->get('/posts', function () use ($app) {
return $app->encoder->encodeData(Post::find());
});
$app->get('/authors', function () use ($app) {
return $app->encoder->encodeData(Author::find());
});
$app->get('/sites', function () use ($app) {
return $app->encoder->encodeData(Site::find());
});
$app->get('/comments', function () use ($app) {
return $app->encoder->encodeData(Comment::find());
});
$app->options('*');
//-----------------------------------------------
// Begin Middleware
//-----------------------------------------------
$app->before(function () use ($app) {
$app->response->setContentType('application/vnd.api+json')->sendHeaders();
});
$app->after(function () use ($app) {
$content = $app->getReturnedValue();
switch (true) {
case is_string($content):
$app->response->setContent($content);
break;
case is_array($content):
$app->response->setJsonContent($content);
break;
}
});
$app->finish(function () use ($app) {
if (!$app->response->isSent()) {
$app->response->sendHeaders()->send();
}
});
/**
* Not found handler
*/
$app->notFound(function () use ($app) {
$app->response->setStatusCode(404, null)->sendHeaders();
echo 'Not Found';
});
/**
* Error handlers
*/
$app->error(function (\Exception $e) {
// TODO: Implement error handler
return true;
});