-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.php
86 lines (66 loc) · 2.26 KB
/
app.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
81
82
83
84
85
86
<?php
require_once __DIR__ . '/config/config.php';
require_once ROOT_PATH . '/vendor/autoload.php';
use Slim\Slim;
use Slim\Route;
$app = new Slim(array(
'debug' => true,
'templates.path' => ROOT_PATH . '/templates',
'mode' => 'development',
'log.enabled' => true
));
$oDB = new PDO('sqlite:' . SQLITE_FILE);
$oDB->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// ------- ROUTE SETTINGS -------
Route::setDefaultConditions(array(
'id' => '\d+',
'itemId' => '\d+'
));
// ------- HOOKS -------
$app->hook('slim.before.dispatch', function() use ($app) {
if (!$app->request->isAjax()) {
$app->render('header.php');
}
});
$app->hook('slim.after.dispatch', function() use ($app) {
if (!$app->request->isAjax()) {
$app->render('footer.php');
}
});
// ------- ITEMS -------
$app->get('/', function() {});
$app->get('/items', function() use ($app, $oDB) {
$stm = $oDB->query("SELECT * FROM item");
$items = $stm->fetchall(\PDO::FETCH_OBJ);
$app->response->setStatus(200);
$app->response->headers->set('Content-Type', "application/json");
$app->response->setBody(json_encode($items));
});
$app->post('/items', function() use ($app, $oDB) {
$body = json_decode($app->request->getBody());
$stm = $oDB->prepare("INSERT INTO item VALUES (NULL, ?)");
$stm->execute([$body->name]);
$id = $oDB->lastInsertId();
$app->response->setStatus(201);
$app->response->headers->set('Location', '/items/' . $id);
});
$app->get('/items/:id', function($id) use ($app, $oDB) {
$stm = $oDB->prepare("SELECT * FROM item WHERE id = ?");
$stm->execute([$id]);
$item = $stm->fetch(\PDO::FETCH_OBJ);
$app->response->setStatus(200);
$app->response->headers->set('Content-Type', "application/json");
$app->response->setBody(json_encode($item));
});
$app->put('/items/:id', function($id) use ($app, $oDB) {
$body = json_decode($app->request->getBody());
$stm = $oDB->prepare("UPDATE item SET name = ? WHERE id = ?");
$stm->execute([$body->name, $id]);
$app->response->setStatus(204);
});
$app->delete('/items/:id', function($id) use ($app, $oDB) {
$stm = $oDB->prepare("DELETE FROM item WHERE id = ?");
$stm->execute([$id]);
$app->response->setStatus(200);
});
$app->run();