-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.class.php
111 lines (103 loc) · 2.61 KB
/
router.class.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
/**
* Basic Router
*
* This file contains the Basic Router class
* @author Jurjen Beukenhorst <info@juje007.be>
* @version 1.0
* @package basic-router
*/
/**
* The Basic Router class itself.
* This class handles basic router settings
* @package basic-router
*/
class Router {
/**
* $pages holds all of the pages in a array
* @access private
* @var array
*/
var $pages;
/**
* __construct creates a fallback for the Home page and the error page
* @return void
*/
function __construct() {
$this->newPage('Home', 'home', function(){
echo 'This is home';
});
$this->newPage('Error', 'error', function(){
echo 'Something went wrong';
});
}
/**
* newPage() is used to add a new page
* @param string $page_name The page name
* @param string $page_slug The page slug
* @param obj $call_back This is called when the page is called
* @return void
*/
function newPage($page_name, $page_slug, $call_back) {
$this->pages[$page_slug] = array(
'name' => $page_name,
'call_back' => $call_back
);
}
/**
* getContent() is used to get the page title and the page content
* @param string $input The page to get
* @param string $title_syntax The title syntax (%title% is used)
* @return array An array with the content and title
*/
function getContent($input, $title_syntax = '') {
if(empty($input)) {
$content = $this->pages['home']['call_back'];
$title = $this->pages['home']['name'];
} else {
if(array_key_exists($input, $this->getPages())) {
$content = $this->pages[$input]['call_back'];
$title = $this->pages[$input]['name'];
} else {
$content = $this->pages['error']['call_back'];
$title = $this->pages['error']['name'];
}
}
return array(
'content' => $content,
'title' => str_replace('%title%', $title, $title_syntax)
);
}
/**
* getMenu() gets the menu
* @param string $mainPage The main page for the url also seen as prefix
* @param string $slug_syntax The slug syntax (%slug% is used)
* @return array An array with the menu items
*/
function getMenu($mainPage, $slug_syntax = '') {
$return = array();
foreach($this->getPages() as $slug => $name) {
if($slug !== 'error') {
if($slug === 'home') {
$return[$mainPage] = $name;
} else {
$return[$mainPage . str_replace('%slug%', $slug, $slug_syntax)] = $name;
}
}
}
return $return;
}
/**
* getPages() is used to get an array with all the pages
* @access private
* @return array
*/
private function getPages() {
$retrun = array();
foreach($this->pages as $key => $val) {
$return[$key] = $val['name'];
}
return $return;
}
}
?>