-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.php
60 lines (47 loc) · 1.5 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
58
59
60
<?php
require 'vendor/autoload.php';
require 'bootstrap.php';//have $em
require 'models/article.php';//Article Model
$app = new \Slim\Slim();
$app->config(array(
'debug' => DEBUG,
'templates.path' => 'views'
));
$app->get('/', function() use ($app) {
$app->render("index.php");
});
// articles group
$app->group('/articles', function () use ($app, $em) {
// Get all articles
$app->get('/', function () use ($app, $em) {
$all = Article::createQuery('a')->getArrayResult();
$app->response->setBody(json_encode($all));
});
// Post create article
$app->post('/', function () use ($app, $em) {
$article = Article::createFromJson($app->request->getBody());
$article->persist();
$em->flush();
$app->response->setBody($article->toJson());
});
// Get article with ID
$app->get('/:id', function ($id) use ($app, $em) {
$article = Article::find($id);
$app->response->setBody($article->toJson());
});
// Update article with ID
$app->put('/:id', function ($id) use ($app, $em) {
$article = Article::find($id);
$article->updateFromJson($app->request->getBody());
$em->flush();
$app->response->setBody($article->toJson());
});
// Delete article with ID
$app->delete('/:id', function ($id) use ($app, $em) {
$article = Article::find($id);
$em->remove($article);
$em->flush();
$app->response->setBody($article->toJson());
});
});
$app->run();