/
index.php
executable file
·57 lines (45 loc) · 1.34 KB
/
index.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
<?php
// In case one is using PHP 5.4's built-in server
$filename = __DIR__ . preg_replace('#(\?.*)$#', '', $_SERVER['REQUEST_URI']);
if (php_sapi_name() === 'cli-server' && is_file($filename)) {
return false;
}
// Include the Router class
require_once __DIR__ . '/core/Router.php';
// Include configuration and models
require_once __DIR__ . '/includes/config.php';
require_once __DIR__ . '/core/Model.php';
require_once __DIR__ . '/model/Post.php';
require_once __DIR__ . '/model/FeedSource.php';
// Create a Router
$router = new \Bramus\Router\Router();
// Custom 404 Handler
$router->set404(function () {
header($_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found');
echo '404, route not found!';
});
// Before Router Middleware
$router->before('GET', '/.*', function () {
header('Content-Type: application/json');
});
// // Static route: / (homepage)
// $router->get('/', function () {
// echo '';
// });
$router->mount('/post', function () use ($router) {
// Route: /posts (fetch all posts)
$router->get('/', function () {
$postModel = new Post();
$posts = $postModel->getAllPosts();
echo json_encode($posts);
});
// Route: /post/id (fetch a single post)
$router->get('/(\d+)', function ($id) {
$postModel = new Post();
$post = $postModel->getPost($id);
echo json_encode($post);
});
});
// Thunderbirds are go!
$router->run();
// EOF