Ik ben wat aan het uitproberen namelijk het MVC pattern.
Hiervoor heb ik een heleboel informatie op het net gevonden echter zit ik nog met 1 probleem waar ik geen oplossing voor vind.
Ik maak gebruik van een single point of entry (index.php) en een router.class.php zorgt ervoor dat de correcte controller en view aangesproken wordt. Echter heb ik nu een probleem wanneer er bv in een css file een url link staat naar een image... Deze wordt ook via de router gestuurd waar hij een controller voor zoekt en uiteindelijk een 404 melding geeft ipv de image... Hoe los ik dit nu op?
.htaccess:
router.class.php
Hiervoor heb ik een heleboel informatie op het net gevonden echter zit ik nog met 1 probleem waar ik geen oplossing voor vind.
Ik maak gebruik van een single point of entry (index.php) en een router.class.php zorgt ervoor dat de correcte controller en view aangesproken wordt. Echter heb ik nu een probleem wanneer er bv in een css file een url link staat naar een image... Deze wordt ook via de router gestuurd waar hij een controller voor zoekt en uiteindelijk een 404 melding geeft ipv de image... Hoe los ik dit nu op?
.htaccess:
code:
1
2
3
4
5
6
| RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php?rt=$1 [L,QSA] |
router.class.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
| <?php class router { private $registry; private $path; public $file; public $controller; public $action; public function __construct($registry) { $this->registry = $registry; } public function setPath($path) { if(is_dir($path) == false){ FB::warn('Invalid controller path: `' . $path . '`', 'Router'); return false; } FB::info($path, 'path'); $this->path = $path; } public function loader(){ /*** check the route ***/ $this->getController(); /*** if the file is not there diaf ***/ if (is_readable($this->file) == false){ $this->file = $this->path.'/error404.php'; $this->controller = 'error404'; } /*** include the controller ***/ include $this->file; /*** a new controller class instance ***/ $class = $this->controller . 'Controller'; $controller = new $class($this->registry); /*** check if the action is callable ***/ if (is_callable(array($controller, $this->action)) == false){ $action = 'index'; }else{ $action = $this->action; } /*** run the action ***/ $controller->$action(); } private function getController() { /*** get the route from the url ***/ $route = (empty($_GET['rt'])) ? '' : $_GET['rt']; if (empty($route)){ $route = 'index'; }else{ /*** get the parts of the route ***/ $parts = explode('/', $route); $this->controller = $parts[0]; if(isset( $parts[1])){ $this->action = $parts[1]; } } if (empty($this->controller)){ $this->controller = 'index'; } /*** Get action ***/ if (empty($this->action)){ $this->action = 'index'; } FB::info($this->action, 'Router action'); /*** set the file path ***/ $this->file = $this->path .'/'. $this->controller . 'Controller.php'; } } ?> |