Passed
Branch master (63c584)
by Marwan
01:51
created

index.php (3 issues)

1
<?php
2
3
require '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
    $message = Globals::getPost('message');
15
    if ($message) {
16
        return htmlspecialchars($message, ENT_QUOTES);
17
    }
18
}, 'POST');
19
20
Router::handle('/contact', function ($path, $match, $previous) {
21
    return View::render('contact', [
22
        'title' => 'Contact',
23
        'message' => $previous ?? null
24
    ]);
25
}, ['GET', 'POST']);
26
27
28
29
$controller = new \App\Controller\DefaultController();
30
Router::handle('/example', [$controller, 'exampleAction']);
31
32
33
34
Router::post('/not-allowed', function () {
35
    // '/not-allowed' will be forwarded to Router::handleMethodNotAllowed()
36
    // when visited via browser (if request method is not POST)
37
38
    // '/not-found' will be forwarded to Router::handleRouteNotFound()
39
});
40
41
Router::handle('/development-exception', function () {
42
    Config::set('global.env', 'DEVELOPMENT');
43
44
    throw new \Exception('Test!');
45
});
46
47
Router::handle('/production-exception', function () {
48
    Config::set('global.env', 'PRODUCTION');
49
    Config::set('global.errorPage', null);
50
51
    throw new \Exception('Test!');
52
});
53
54
55
56
Router::handle('/redirect', function () {
57
    // this will take place in client's browser (client knows about it).
58
    Router::redirect('/');
59
});
60
61
Router::handle('/forward', function () {
62
    // this will take place in the application (client does not know about it).
63
    Router::forward('/');
64
});
65
66
67
68
// this will match anything after the slash
69
// Router::get('/dump/{var?}', 'dd');
70
Router::any('/dump/{var?}', function ($path, $match, $previous) {
71
    return View::render('dump', ['vars' => compact('path', 'match', 'previous')], 'simple');
72
});
73
// this will match numbers only
74
Router::get('/number/([0-9]+)', function ($path, $match) {
75
    Router::forward('/dump/' . $match);
76
});
77
// this will match letters only
78
Router::get('/string/([a-z]+)', function ($path, $match) {
79
    Router::forward('/dump/' . $match);
80
});
81
82
83
84
Router::handleRouteNotFound(function ($path) {
85
    return View::render('error/404', compact('path'));
86
});
87
88
Router::handleMethodNotAllowed(function ($path, $method) {
89
    return View::render('error/405', compact('path', 'method'));
90
});
91
92
93
94
// registers all pages configured in "./config/data.php"
95
foreach ((array)Data::get('pages') as $page) {
96
    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 ignore-unused  annotation

96
    Router::handle($page['route'], function (/** @scrutinizer ignore-unused */ $path, $match, $previous) use ($page) {

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 ignore-unused  annotation

96
    Router::handle($page['route'], function ($path, /** @scrutinizer ignore-unused */ $match, $previous) use ($page) {

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 $previous 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 ignore-unused  annotation

96
    Router::handle($page['route'], function ($path, $match, /** @scrutinizer ignore-unused */ $previous) use ($page) {

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...
97
        return View::render(
98
            $page['page'],
99
            $page['variables'],
100
            $page['layout']
101
        );
102
    }, $page['method']);
103
}
104
105
106
107
Router::start();
108