-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
79 lines (51 loc) · 1.92 KB
/
Copy pathindex.php
File metadata and controls
79 lines (51 loc) · 1.92 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
<?php
require_once __DIR__.'/vendor/autoload.php';
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
$app = new Silex\Application();
$app['debug'] = true;
$app['db'] = function() {
return new \PDO('mysql:host=localhost;dbname=angular','root');
};
$app->get('/', function() use ($app) {
return new Response(file_get_contents('pessoas/templates/template.html'), 200);
});
$app->post('/pessoas', function(Request $request) use ($app) {
$data = $request->getContent();
parse_str($data, $out);
$stmt = $app['db']->prepare("insert into pessoas(nome,email) value(:nome, :email)");
$stmt->bindParam('nome', $out['nome']);
$stmt->bindParam('email', $out['email']);
$stmt->execute();
return $app->json(array('success'=>true));
});
$app->get('/pessoas', function() use ($app) {
$stmt = $app['db']->query("Select * from pessoas");
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $app->json($result);
});
$app->get('/pessoas/{id}', function($id) use ($app) {
$stmt = $app['db']->prepare("Select * from pessoas where id=:id");
$stmt->bindParam('id',$id);
$stmt->execute();
$result = $stmt->fetch(PDO::FETCH_ASSOC);
return $app->json($result);
});
$app->put('/pessoas/{id}', function(Request $request, $id) use ($app) {
$data = $request->getContent();
print_r($data);exit;
parse_str($data, $out);
$stmt = $app['db']->prepare("update pessoas set nome=:nome, email=:email where id=:id");
$stmt->bindParam('id',$id);
$stmt->bindParam('nome', $out['nome']);
$stmt->bindParam('email', $out['email']);
$stmt->execute();
return $app->json(array('success'=>true));
});
$app->delete('/pessoas/{id}', function($id) use ($app) {
$stmt = $app['db']->prepare("delete from pessoas where id=:id");
$stmt->bindParam('id',$id);
$stmt->execute();
return $app->json(array('success'=>true));
});
$app->run();