php class Qeek_Router private base_url private controllers_path Constr

 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
<?php
class Qeek_Router {
private $base_url;
private $controllers_path;
/* Constructor */
function Qeek_Router($uri, $controllers_path, $base_url){
$this->controllers_path = $controllers_path;
$this->parseUri($uri, $base_url);
}
private function parseUri($uri, $base_url){
/*
* Exclude the base path from request_uri
* and split into arguments
*/
$uri = explode($base_url, $uri);
$args = $uri[1];
$args = explode('/', $args);
if(!isset($args[1])) $args[1] = 'Home';
if(!isset($args[2])) $args[2] = 'Index';
$this->runController($args[1], $args[2] , $args);
}
private function runController($controller, $action, $args){
//transform the controller name into a capitalised word
$controller = strtolower($controller);
$controller = ucfirst($controller);
//check if the file exists and run the controller
try{
$controller_file = $this->controllers_path.'/'.$controller.'.php';
if(!file_exists($controller_file))
throw new Exception($controller_file.' not found!');
require_once($controller_file);
//take the form of 'HomeController'
$controller = $controller.'Controller';
new $controller;
} catch(Exception $e){
echo $e->getMessage();
}
}
}
?>