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