-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
76 lines (61 loc) · 2.02 KB
/
index.php
File metadata and controls
76 lines (61 loc) · 2.02 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
<?php
require __DIR__ . '/../vendor/autoload.php';
// create new instance
$router = new \Ken\Router\Router();
// define not found handler
$router->setNotFoundHandler(function($params = []) {
echo "Route '" . $params['route'] . "' not found.";
});
// define routes
$router->get('/', function($params = []) {
echo 'Index page';
});
// Route group
$router->group('/users', function() use ($router) {
$router->get('/', function($params = []) {
echo 'Show a list of user<br>';
});
// Route with named parameter
$router->group('/{name}', function() use ($router) {
$router->get('/', function($params = []) {
echo 'Show user : ' . $params['name'] . '<br>';
});
$router->get('/trx[/{id}]', function($params = []) {
if (isset($params['id'])) {
echo 'Show transaction ID ' . $params['id'] . ' of user '. $params['name'] .'<br>';
} else {
echo 'Show all transaction of user '. $params['name'] .'<br>';
}
});
});
}, [
'before' => [function() {
echo 'This is executed before handler<br>';
}],
'after' => [function() {
echo 'This is executed after handler<br>';
}],
]);
// Get route path
$routePath = $_SERVER['PATH_INFO'] ?? '/';
// Get request method
$requestMethod = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : 'GET';
// Resolves route
$routeObject = $router->resolve($routePath, $requestMethod);
// If $routeObject is not null
if($routeObject) {
if (isset($routeObject['before'])) {
foreach ($routeObject['before'] as $before) {
call_user_func($before);
}
}
// You can add some custom parameters here, like HttpRequest and HttpResponse object
call_user_func_array($routeObject['handler'], [$routeObject['params']]);
if (isset($routeObject['after'])) {
foreach ($routeObject['after'] as $after) {
call_user_func($after);
}
}
} else {
echo "Route '" . $routePath . "' not found.";
}