-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathindex.php
More file actions
103 lines (90 loc) · 2.71 KB
/
index.php
File metadata and controls
103 lines (90 loc) · 2.71 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
require 'vendor/autoload.php';
//configuração de conexão com banco de dados
$pdo = new PDO('mysql:dbname=slim_helloworld;host:127.0.0.1','root','');
$db = new NotORM($pdo);
//criando uma nova instancia do Slim
$app = new \Slim\Slim(array(
'MODE' => 'development',
'TEMPLATE.PATH' => './templates'
));
//setando o encoding
$app->contentType('text/html; charset=utf-8');
//rota padrão para página não encontrada
$app->notFound(function () use ($app) {
$app->response()->header('Content-Type', 'application/json');
echo json_encode(array(
'status' => false,
'message' => 'The resource doesn\'t exist. Check the API docs'
));
//$app->render('404.php'); //template padrão para erro 404
});
//criando uma nova Rota
$app->get('/', function() use($app){
$app->view()->setData('title', 'Olá Mundo');
$app->render('index.php');
//echo "Hello Slim World";
});
//obtendo todos os livros
$app->get('/books', function() use ($app, $db){
$books = array();
foreach ($db->books() as $book) {
$books[] = array(
'id' => $book['id'],
'title' => $book['title'],
'author' => $book['author'],
'summary' => utf8_encode($book['summary'])
);
}
$app->response()->header('Content-Type', 'application/json');
echo json_encode($books);
});
//obtendo um livro pelo id
$app->get('/books/:key/:id', function($key, $id) use ($app,$db){
//cabeçalho da resposta
$app->response()->header('Content-Type', 'aplication/json');
//setando o id do livro para busca
$book = $db->books()->where('id', $id);
//$book = $db->books('id = ?', $id);
if($data = $book->fetch()){
echo json_encode(array(
'id' => $data['id'],
'title' => $data['title'],
'author' => $data['author'],
'summary' => utf8_encode($data['summary'])
));
}else{
echo json_encode(array(
'status' => false,
'message' => 'Book ID $id does not existe'
));
}
});
//adicionando um novo livro
$app->post('/book', function() use ($app, $db){
$app->response()->header('Content-Type', 'application/json');
$book = utf8_encode($app->request()->post());
$result = $db->books->insert($book);
echo json_enconde(array('id'=>$result['id']));
});
//editanto um livro
$app->put('/books/:id', function($id) use($app, $db){
$app->response()->header('Content-Type', 'application/json');
$book = $db->books()->where('id',$id);
if($book->fetch()){
$post = $app->request->put();
$result = $book->update($post);
echo json_encode(array(
'status' => (bool)$result,
'message' => 'Book updated successfully'
));
}else{
echo json_encode(array(
'status' => false,
'message' => 'Book id $id does not exist'
));
}
});
//rodando a aplicação
$app->run();
?>