-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathroutes.php
44 lines (40 loc) · 1.55 KB
/
routes.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
<?php
function call($controller, $action) {
// require the file that matches the controller name
require_once('controllers/' . $controller . '_controller.php');
// create a new instance of the needed controller
switch($controller) {
//for non-data-driven pages use the PagesController class
case 'pages':
$controller = new PagesController();
break;
default:
//for all data-driven pages use a specific Controller class
//we need the model to query the database later in the process
require_once("models/{$controller}.php");
$controllerClassName = $controller . 'Controller';
$controller = new $controllerClassName();
break;
}
// call the requested action
$controller->{ $action }();
}
// for validation we list the allowed controllers and their actions
// Add an entry for each new controller and its actions
$controllers = array('pages' => ['home', 'error'],
'product' => ['readAll','read','create','update','delete'],
'controllerXXX' => ['actionYYY', 'actionZZZ'],
);
// check that the requested controller and action are both allowed
// if someone tries to access something else they will be redirected
// to the error action of the pages controller
if (array_key_exists($controller, $controllers)) {
if (in_array($action, $controllers[$controller])) {
call($controller, $action);
} else {
call('pages', 'error');
}
} else {
call('pages', 'error');
}
?>