-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.php
114 lines (86 loc) · 2.99 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
require_once 'vendor/Teeny.php';
// Uncomment next line for composer projects
# require_once 'vendor/autoload.php';
$app = new Inphinit\Teeny;
// Uncomment next lines for "stress test" (fake paths)
# for ($i = 0; $i < 300; ++$i) {
# $app->action('GET', '/foooz/<a' . $i . '>', 'examples/home.php' . $i);
# }
$app->action('ANY', '/', 'examples/home.php');
$app->action('GET', '/include', 'examples/foo.php');
$app->action('GET', '/info', 'phpinfo');
$app->action('GET', '/about', function () {
echo 'Teeny is very small and easy route system';
});
$app->action('GET', '/memory', function () {
echo 'memory peak usage: ', round(memory_get_peak_usage() / 1024 / 1024, 3), 'MB';
});
$app->action('POST', '/foo/bar', function () {
echo 'Hello foo bar!';
});
$app->action('PUT', '/cat', function () use ($app) {
$app->status(201);
echo 'Resource created';
});
$app->action('GET', '/foo/<foo>/<bar>', function ($params) {
echo 'response from /<foo>/<bar>';
echo '<pre>';
print_r($params);
echo '</pre>';
});
$app->action('GET', '/foo/<foo>-<bar>', function ($params) {
echo 'response from /<foo>-<bar>';
echo '<pre>';
print_r($params);
echo '</pre>';
});
// Example: http://localhost:8000/article/foo-1000
$app->action('GET', '/article/<name>/<id>', function ($params) use ($app) {
if (ctype_digit($params['id'])) {
echo 'Article ID: ', $params['id'], '<br>';
echo 'Article name: ', $params['name'];
} else {
$app->status(400);
echo 'Invalid URL';
}
});
// Example: http://localhost:8000/blog/foo-1000
$app->action('GET', '/blog/<name>-<id:num>', function ($params) {
echo 'Article ID: ', $params['id'], '<br>';
echo 'Article name: ', $params['name'];
});
// Example: http://localhost:8000/test/foo-1000
$app->action('GET', '/test/<id:num>', 'testCallback');
// Example: http://localhost:8000/test/foo/abc
$app->action('GET', '/test/foo/<name:alpha>', 'testCallback');
// Example: http://localhost:8000/test/bar/f0f0f0
$app->action('GET', '/test/bar/<barcode:alnum>', 'testCallback');
$app->action('GET', '/decimal/<value:decimal>', 'testCallback');
$app->action('GET', '/uuid/<value:uuid>', 'testCallback');
$app->action('GET', '/version/<value:version>', 'testCallback');
function testCallback($params) {
echo '<h1>Results testCallback():</h1>';
echo '<pre>';
print_r($params);
echo '</pre>';
}
$app->action('GET', '/nospace/<value:nospace>', function ($params) {
echo '<h1>nospace</h1>';
echo '<pre>';
print_r($params);
echo '</pre>';
});
// custom pattern
$app->action('GET', '/custom/<myexample:example>', function ($params) use ($app) {
echo '<h1>custom pattern</h1>';
echo '<pre>';
print_r($params);
echo '</pre>';
});
$app->setPattern('example', '[A-Z]\d+');
// Handle the HTTP response when the code is different than 200
$app->handlerCodes(array(403, 404, 405), function ($params) {
echo 'Custom page error ', $params['code'];
});
return $app->exec();