aboutsummaryrefslogtreecommitdiff
path: root/app/class/routes.php
blob: 71b75d62151e872a1366415281e35adcfd09674e (plain)
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
<?php


class Routes
{
    /**
     * Cherche une correspondance entre l'URL et les routes, et appelle la méthode appropriée
     */
    public function match()
    {
        $router = new AltoRouter();
        if(!empty(Config::basepath())) {
            $router->setBasePath(DIRECTORY_SEPARATOR . Config::basepath());
        }
        $router->addMatchTypes(array('cid' => '[a-zA-Z0-9-_+,\'!%@&.$€=\(\|\)]+'));
        $router->addRoutes([
            ['GET|POST', '/', 'Backrouter#run', 'backrouter'],
            ['GET', '/[cid:art]/', 'Controllerart#read', 'artread/'],
            ['GET', '/[cid:art]', 'Controllerart#read', 'artread'],
            ['GET', '/[cid:art]/add', 'Controllerart#add', 'artadd'],
            ['GET', '/[cid:art]/edit', 'Controllerart#edit', 'artedit'],
            ['GET', '/[cid:art]/log', 'Controllerart#log', 'artlog'],
            ['POST', '/[cid:art]/edit', 'Controllerart#update', 'artupdate'],
            ['GET', '/[cid:art]/delete', 'Controllerart#confirmdelete', 'artconfirmdelete'],
            ['POST', '/[cid:art]/delete', 'Controllerart#delete', 'artdelete'],
        ]);

        $match = $router->match();
        if ($match) {
            $callableParts = explode('#', $match['target']);
            $controllerName = $callableParts[0];
            $methodName = $callableParts[1];

            $controller = new $controllerName($router);
			
            call_user_func_array(array($controller, $methodName), $match['params']);
        }
		//404
        else {
            if(!empty(Config::route404())) {
                $controller = new Controller($router);
                $controller->routedirect('artread/', ['art' => Config::route404()]);
            } else {
                header($_SERVER["SERVER_PROTOCOL"] . ' 404 Not Found');
            }
        }
    }
}