|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
require_once 'bootstrap/autoload.php'; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
Router::handle('/', function () { |
|
8
|
|
|
return View::render('home', ['title' => 'Home']); |
|
9
|
|
|
}); |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
Router::middleware('/contact', function () { |
|
14
|
|
|
if (isset($_POST['message']) && !empty($_POST['message'])) { |
|
15
|
|
|
return htmlspecialchars($_POST['message'], ENT_QUOTES); |
|
16
|
|
|
} |
|
17
|
|
|
}, 'POST'); |
|
18
|
|
|
|
|
19
|
|
|
Router::handle('/contact', function ($path, $match, $previous) { |
|
20
|
|
|
return View::render('contact', [ |
|
21
|
|
|
'title' => 'Contact', |
|
22
|
|
|
'message' => $previous ?? null |
|
23
|
|
|
]); |
|
24
|
|
|
}, ['GET', 'POST']); |
|
25
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
$controller = new \App\Controller\DefaultController(); |
|
29
|
|
|
Router::handle('/example', [$controller, 'exampleAction']); |
|
30
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
Router::post('/not-allowed', function () { |
|
34
|
|
|
// '/not-allowed' will be forwarded to Router::handleMethodNotAllowed() |
|
35
|
|
|
// when visited via browser (if request method is not POST) |
|
36
|
|
|
|
|
37
|
|
|
// '/not-found' will be forwarded to Router::handleRouteNotFound() |
|
38
|
|
|
}); |
|
39
|
|
|
|
|
40
|
|
|
Router::handle('/development-exception', function () { |
|
41
|
|
|
Config::set('global.env', 'DEVELOPMENT'); |
|
42
|
|
|
|
|
43
|
|
|
throw new \Exception('Test!'); |
|
44
|
|
|
}); |
|
45
|
|
|
|
|
46
|
|
|
Router::handle('/production-exception', function () { |
|
47
|
|
|
Config::set('global.env', 'PRODUCTION'); |
|
48
|
|
|
Config::set('global.errorPage', null); |
|
49
|
|
|
|
|
50
|
|
|
throw new \Exception('Test!'); |
|
51
|
|
|
}); |
|
52
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
|
|
55
|
|
|
Router::handle('/redirect', function () { |
|
56
|
|
|
// this will take place in client's browser (client knows about it). |
|
57
|
|
|
Router::redirect('/'); |
|
58
|
|
|
}); |
|
59
|
|
|
|
|
60
|
|
|
Router::handle('/forward', function () { |
|
61
|
|
|
// this will take place in the application (client does not know about it). |
|
62
|
|
|
Router::forward('/'); |
|
63
|
|
|
}); |
|
64
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
|
|
67
|
|
|
// this will match anything after the slash |
|
68
|
|
|
// Router::get('/dump/{var?}', 'dd'); |
|
69
|
|
|
Router::any('/dump/{var?}', function ($path, $match, $previous) { |
|
70
|
|
|
return View::render('dump', ['vars' => compact('path', 'match', 'previous')], 'simple'); |
|
71
|
|
|
}); |
|
72
|
|
|
// this will match numbers only |
|
73
|
|
|
Router::get('/number/([0-9]+)', function ($path, $match) { |
|
74
|
|
|
Router::forward('/dump/' . $match); |
|
75
|
|
|
}); |
|
76
|
|
|
// this will match letters only |
|
77
|
|
|
Router::get('/string/([a-z]+)', function ($path, $match) { |
|
78
|
|
|
Router::forward('/dump/' . $match); |
|
79
|
|
|
}); |
|
80
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
|
|
83
|
|
|
Router::handleRouteNotFound(function ($path) { |
|
84
|
|
|
return View::render('error/404', compact('path')); |
|
85
|
|
|
}); |
|
86
|
|
|
|
|
87
|
|
|
Router::handleMethodNotAllowed(function ($path, $method) { |
|
88
|
|
|
return View::render('error/405', compact('path', 'method')); |
|
89
|
|
|
}); |
|
90
|
|
|
|
|
91
|
|
|
|
|
92
|
|
|
|
|
93
|
|
|
// registers all pages configured in "./config/data.php" |
|
94
|
|
|
foreach ((array)Data::get('pages') as $page) { |
|
95
|
|
|
Router::handle($page['route'], function ($path, $match, $previous) use ($page) { |
|
|
|
|
|
|
96
|
|
|
return View::render( |
|
97
|
|
|
$page['page'], |
|
98
|
|
|
$page['variables'], |
|
99
|
|
|
$page['layout'] |
|
100
|
|
|
); |
|
101
|
|
|
}, $page['method']); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
|
|
105
|
|
|
|
|
106
|
|
|
Router::start(); |
|
107
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.