Ik gebruik nu al een jaar het simpele MVC-framework van "phpit.net". (Spijtig genoeg ligt hun site al enige tijd plat)
Dit werkt goed met apache op linux (Gentoo), maar weigert te werken onder windows.
Aangezien dat Mod-rewrite aan staat, is mijn eerste idee dat het iets te maken heeft met de 'directories' van windows en linux. Meerbepaald het verschil tussen slashes en backslashes.
Error tijdens laden pagina op windowsserver:
Startup.php
Index.php
Router.php
Dit werkt goed met apache op linux (Gentoo), maar weigert te werken onder windows.
Aangezien dat Mod-rewrite aan staat, is mijn eerste idee dat het iets te maken heeft met de 'directories' van windows en linux. Meerbepaald het verschil tussen slashes en backslashes.
Error tijdens laden pagina op windowsserver:
code:
1
2
3
4
5
| Fatal error: Uncaught exception 'Exception' with message 'Invalid controller path: `C:\Program Files\htdocs\DEMO\library\mvc\../../application/controllers\`' in C:\Program Files\htdocs\DEMO\library\mvc\classes\router.php:21 Stack trace: #0 C:\Program Files\htdocs\DEMO\index.php(18): Router->setPath('C:\Program File...') #1 {main} thrown in C:\Program Files\htdocs\DEMO\library\mvc\classes\router.php on line 21 |
Startup.php
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
| <?php error_reporting (E_ALL); if (version_compare(phpversion(), '5.1.0', '<') == true) { die ('PHP5.1 Only'); } // Constants: define ('DIRSEP', DIRECTORY_SEPARATOR); // Get site path $site_path = realpath(dirname(__FILE__) . DIRSEP . '..' . DIRSEP) . DIRSEP; define ('site_path', $site_path); // For loading classes function __autoload($class_name) { $filename = strtolower($class_name) . '.php'; $file = site_path . 'classes' . DIRSEP . $filename; if (file_exists($file) == false) { return false; } include ($file); } $registry = new Registry; ?> |
Index.php
PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| <?php // Start sessions session_start(); // Include the config settings require 'config.php'; // Include the templatepower class require( "library/templatepower/class.TemplatePower.inc.php"); // Startup tasks require 'library/mvc/includes/startup.php'; // Load router $router = new Router($registry); $registry->set ('router', $router); $router->setPath (site_path . '../../application/controllers'); $router->delegate(); ?> |
Router.php
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
| <?php /** The Router class has to analyze the request, and then load the correct command. **/ Class Router { private $registry; private $path; private $args = array(); function __construct($registry) { $this->registry = $registry; } function setPath($path) { $path = trim($path, '/\\'); $path .= DIRSEP; if (is_dir("/".$path) == false) { throw new Exception ('Invalid controller path: `' . $path . '`'); } $this->path = "/".$path; } function getArg($key) { if (!isset($this->args[$key])) { return null; } return $this->args[$key]; } function delegate() { // Analyze route $this->getController($file, $controller, $action, $args); // File available? if (is_readable($file) == false) { $this->notFound('no-file'); } // Include the file include ($file); // Initiate the class $class = 'Controller_' . $controller; $controller = new $class($this->registry); // Action available? if (is_callable(array($controller, $action)) == false) { $this->notFound('no-action'); } // Run action $controller->$action($args); } private function extractArgs($args) { if (count($args) == 0) { return false; } $this->args = $args; } private function getController(&$file, &$controller, &$action, &$args) { $route = (empty($_GET['route'])) ? '' : $_GET['route']; if (empty($route)) { $route = 'index'; } // Get separate parts $route = trim($route, '/\\'); $parts = explode('/', $route); // Find right controller $cmd_path = $this->path; foreach ($parts as $part) { $fullpath = $cmd_path . $part; // Is there a dir with this path? if (is_dir($fullpath)) { $cmd_path .= $part . DIRSEP; array_shift($parts); continue; } // Find the file if (is_file($fullpath . '.php')) { $controller = $part; array_shift($parts); break; } } if (empty($controller)) { $controller = 'index'; }; // Get action $action = array_shift($parts); if (empty($action)) { $action = 'index'; } $file = $cmd_path . $controller . '.php'; $args = $parts; } private function notFound() { die("404 Not Found"); } } ?> |