php class FrontController private controllerPath lib actions private c

 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
<?php
class FrontController
{
private $controllerPath = './lib/actions';
private $controller = '';
private $action = '';
public function __construct()
{ }
public function run()
{
// Get request
$this->prepareRequest();
// try {
// Exec controller and actions
$this->exec();
// } catch (Exception $e) {
// }
}
public function prepareRequest()
{
$param = explode('/', strtolower(Env::Get('q', Env::TYPE_STRING, '')));
$this->controller = ucfirst(array_shift($param));
$this->action = array_shift($param);
$this->controller = ($this->controller) ? $this->controller : 'Index';
$this->action = ($this->action) ? $this->action : 'index';
}
public function getController()
{
return $this->controller . 'Controller';
}
public function getAction()
{
return $this->action . 'Action';
}
/**
* Execute controller
*/
protected function exec()
{
//var_dump($this->getController());
// Check the controller is exists
if (!class_exists($this->getController(), true)) {
throw new Exception('The requested URL, "' . Env::Server('REQUEST_URI') . '" was not found on this server.', 404); }
$class = $this->getController();
$controller = new $class();
if ( !method_exists($controller, $this->getAction() ) ) {
throw new RuntimeException ("Action is not: " . $this->getAction());
}
$controller->{$this->getAction()}();
}
}
?>