bzr branch
http://gegoxaren.bato24.eu/bzr/%2Bjunk/StupidMVC
|
1
by Gustav Hartvigsson
* initial code |
1 |
<?php
|
2 |
||
|
3
by Gustav Hatvigsson
* Made the router a class |
3 |
class Router { |
|
10
by Gustav Hartvigsson
* __contructor -> __construct |
4 |
|
|
3
by Gustav Hatvigsson
* Made the router a class |
5 |
private $list_controllers = []; |
|
10
by Gustav Hartvigsson
* __contructor -> __construct |
6 |
|
|
3
by Gustav Hatvigsson
* Made the router a class |
7 |
private $default_controller = null; |
|
10
by Gustav Hartvigsson
* __contructor -> __construct |
8 |
|
|
4
by Gustav Hatvigsson
* Added FourOhFour.php |
9 |
private $four_oh_four_controller = null; |
|
10
by Gustav Hartvigsson
* __contructor -> __construct |
10 |
|
|
4
by Gustav Hatvigsson
* Added FourOhFour.php |
11 |
public function |
12 |
register_controller ($controller_name) { |
|
|
3
by Gustav Hatvigsson
* Made the router a class |
13 |
if (isset ($list_controllers[$controller_name])) { |
14 |
return; |
|
15 |
}
|
|
|
10
by Gustav Hartvigsson
* __contructor -> __construct |
16 |
|
|
3
by Gustav Hatvigsson
* Made the router a class |
17 |
require (APP_DIR . "Controllers" . DS . $controller_name . ".php"); |
|
10
by Gustav Hartvigsson
* __contructor -> __construct |
18 |
|
|
3
by Gustav Hatvigsson
* Made the router a class |
19 |
$this->list_controllers[$controller_name] = $controller_name; |
|
10
by Gustav Hartvigsson
* __contructor -> __construct |
20 |
|
|
3
by Gustav Hatvigsson
* Made the router a class |
21 |
}
|
|
10
by Gustav Hartvigsson
* __contructor -> __construct |
22 |
|
|
4
by Gustav Hatvigsson
* Added FourOhFour.php |
23 |
public function |
24 |
register_404_controller ($controller_name) { |
|
25 |
if (isset($this->four_oh_four_controller)){ |
|
26 |
return; |
|
27 |
}
|
|
28 |
$this->four_oh_four_controller = $controller_name; |
|
29 |
$this->register_controller ($controller_name); |
|
30 |
}
|
|
|
10
by Gustav Hartvigsson
* __contructor -> __construct |
31 |
|
|
8
by Gustav Hartvigsson
woops |
32 |
public function |
33 |
register_default_controller ($controller_name) { |
|
|
3
by Gustav Hatvigsson
* Made the router a class |
34 |
if (isset($this->default_controller)) { |
35 |
return; |
|
36 |
}
|
|
37 |
$this->default_controller = $controller_name; |
|
38 |
$this->register_controller ($controller_name); |
|
39 |
}
|
|
|
10
by Gustav Hartvigsson
* __contructor -> __construct |
40 |
|
|
7
by Gustav Hartvigsson
* a few more functions... |
41 |
public function |
42 |
route () { |
|
|
10
by Gustav Hartvigsson
* __contructor -> __construct |
43 |
|
|
3
by Gustav Hatvigsson
* Made the router a class |
44 |
$url = ""; |
45 |
$method = "default_method"; |
|
46 |
$ctrl = $this->default_controller; |
|
47 |
$path = ""; |
|
|
7
by Gustav Hartvigsson
* a few more functions... |
48 |
|
|
3
by Gustav Hatvigsson
* Made the router a class |
49 |
$request_url = ''; |
50 |
if (isset($_SERVER['REQUEST_URI'])) { |
|
51 |
$request_url = $_SERVER['REQUEST_URI']; |
|
52 |
}
|
|
53 |
//echo "Request URL: " . $request_url . "\n";
|
|
54 |
$script_url = ''; |
|
55 |
if (isset($_SERVER['PHP_SELF'])){ |
|
56 |
$script_url = $_SERVER['PHP_SELF']; |
|
57 |
}
|
|
58 |
//echo "Script URL: " . $script_url . "\n";
|
|
|
10
by Gustav Hartvigsson
* __contructor -> __construct |
59 |
|
|
3
by Gustav Hatvigsson
* Made the router a class |
60 |
if($request_url != $script_url) { |
|
4
by Gustav Hatvigsson
* Added FourOhFour.php |
61 |
$url = trim(preg_replace('/'. |
62 |
str_replace('/', '\/', |
|
63 |
str_replace('index.php', '', $script_url)) |
|
64 |
.'/', '', $request_url, 1), '/'); |
|
|
3
by Gustav Hatvigsson
* Made the router a class |
65 |
//echo "url: " . $url . "\n";
|
66 |
}
|
|
|
10
by Gustav Hartvigsson
* __contructor -> __construct |
67 |
|
|
3
by Gustav Hatvigsson
* Made the router a class |
68 |
$parts = explode ('/', $url ); |
|
10
by Gustav Hartvigsson
* __contructor -> __construct |
69 |
|
|
3
by Gustav Hatvigsson
* Made the router a class |
70 |
if (isset ($parts[0]) && $parts[0] != '') { |
71 |
$ctrl = $parts[0]; |
|
72 |
}
|
|
|
10
by Gustav Hartvigsson
* __contructor -> __construct |
73 |
|
|
3
by Gustav Hatvigsson
* Made the router a class |
74 |
if (isset ($parts[1]) && $parts[1] != '') { |
75 |
$method = $parts[1]; |
|
76 |
}
|
|
|
10
by Gustav Hartvigsson
* __contructor -> __construct |
77 |
|
|
3
by Gustav Hatvigsson
* Made the router a class |
78 |
if(isset ($this->list_controllers[$ctrl])) { |
79 |
$controller_obj = new $ctrl (); |
|
80 |
$ctrl_method_list = $controller_obj->get_methods(); |
|
81 |
if ($method != "default_method" && |
|
82 |
isset($ctrl_method_list[$method])) { |
|
83 |
$controller_obj->$ctrl_method_list[$method] (array_slice($parts, 2)); |
|
84 |
return; |
|
|
7
by Gustav Hartvigsson
* a few more functions... |
85 |
}
|
|
4
by Gustav Hatvigsson
* Added FourOhFour.php |
86 |
$controller_obj->default_method (NULL); |
87 |
return; |
|
|
3
by Gustav Hatvigsson
* Made the router a class |
88 |
}
|
|
10
by Gustav Hartvigsson
* __contructor -> __construct |
89 |
|
|
4
by Gustav Hatvigsson
* Added FourOhFour.php |
90 |
$controller_obj = new $this->four_oh_four_controller (); |
91 |
$controller_obj->default_method ($url); |
|
|
10
by Gustav Hartvigsson
* __contructor -> __construct |
92 |
|
|
3
by Gustav Hatvigsson
* Made the router a class |
93 |
}
|
|
1
by Gustav Hartvigsson
* initial code |
94 |
}
|