-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
95 lines (81 loc) · 2.08 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
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
<?php
use Phalcon\Mvc\Micro;
use Phalcon\Loader;
use Phalcon\Di\FactoryDefault;
use Phalcon\Db\Adapter\Pdo\Mysql as PdoMysql;
// Creates the autoloader
$loader = new Loader();
// Register some namespaces
$loader->registerNamespaces(
[
'Model' => __DIR__ . '/models/',
'Controller' => __DIR__ . '/controllers/'
]
);
// Register autoloader
$loader->register();
$di = new FactoryDefault();
// Set up the database service
$di->set(
'db',
function () {
return new PdoMysql(
[
'host' => 'localhost',
'username' => 'root',
'password' => '',
'dbname' => 'umaportodas',
]
);
}
);
$app = new Micro($di);
// Retrieve all routes
$app->get(
'/api/routes',
function () {
// Operation to fetch all the routes
$objRoute = new Controller\Ways();
echo json_encode($objRoute->getAll());
}
);
// Retrieves routes based on primary key
$app->get(
'/api/routes/{id:[0-9]+}',
function ($id) {
// Operation to fetch routes with id $id
$objRoute = new Controller\Ways();
echo json_encode($objRoute->getRoute($id));
}
);
// Adds a new routes
$app->post(
'/api/routes',
function () {
// Operation to create a fresh routes
$objRoute = new Controller\Ways();
echo json_encode($objRoute->create($this->request->getPost()));
}
);
// Updates routes based on primary key
$app->put(
'/api/routes/{id:[0-9]+}',
function ($id) {
// Operation to update a routes with id $id
$objRoute = new Controller\Ways();
echo json_encode($objRoute->update($id, $this->request->getPut()));
}
);
// Deletes routes based on primary key
$app->delete(
'/api/routes/{id:[0-9]+}',
function ($id) {
// Operation to delete the routes with id $id
$objRoute = new Controller\Ways();
echo json_encode($objRoute->delete($id));
}
);
$app->notFound(function () use ($app) {
$app->response->setStatusCode(404, "Not Found")->sendHeaders();
});
$app->handle();