-
Notifications
You must be signed in to change notification settings - Fork 99
/
Application.php
96 lines (79 loc) · 3.64 KB
/
Application.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
<?php
/** For more info about namespaces plase @see http://php.net/manual/en/language.namespaces.importing.php */
namespace Mini\Core;
class Application
{
/** @var null The controller */
private $url_controller = null;
/** @var null The method (of the above controller), often also named "action" */
private $url_action = null;
/** @var array URL parameters */
private $url_params = array();
/**
* "Start" the application:
* Analyze the URL elements and calls the according controller/method or the fallback
*/
public function __construct()
{
// create array with URL parts in $url
$this->splitUrl();
// check for controller: no controller given ? then load start-page
if (!$this->url_controller) {
$page = new \Mini\Controller\HomeController();
$page->index();
} elseif (file_exists(APP . 'Controller/' . ucfirst($this->url_controller) . 'Controller.php')) {
// here we did check for controller: does such a controller exist ?
// if so, then load this file and create this controller
// like \Mini\Controller\CarController
$controller = "\\Mini\\Controller\\" . ucfirst($this->url_controller) . 'Controller';
$this->url_controller = new $controller();
// check for method: does such a method exist in the controller ?
if (method_exists($this->url_controller, $this->url_action) &&
is_callable(array($this->url_controller, $this->url_action))) {
if (!empty($this->url_params)) {
// Call the method and pass arguments to it
call_user_func_array(array($this->url_controller, $this->url_action), $this->url_params);
} else {
// If no parameters are given, just call the method without parameters, like $this->home->method();
$this->url_controller->{$this->url_action}();
}
} else {
if (strlen($this->url_action) == 0) {
// no action defined: call the default index() method of a selected controller
$this->url_controller->index();
} else {
$page = new \Mini\Controller\ErrorController();
$page->index();
}
}
} else {
$page = new \Mini\Controller\ErrorController();
$page->index();
}
}
/**
* Get and split the URL
*/
private function splitUrl()
{
if (isset($_GET['url'])) {
// split URL
$url = trim($_GET['url'], '/');
$url = filter_var($url, FILTER_SANITIZE_URL);
$url = explode('/', $url);
// Put URL parts into according properties
// By the way, the syntax here is just a short form of if/else, called "Ternary Operators"
// @see http://davidwalsh.name/php-shorthand-if-else-ternary-operators
$this->url_controller = isset($url[0]) ? $url[0] : null;
$this->url_action = isset($url[1]) ? $url[1] : null;
// Remove controller and action from the split URL
unset($url[0], $url[1]);
// Rebase array keys and store the URL params
$this->url_params = array_values($url);
// for debugging. uncomment this if you have problems with the URL
//echo 'Controller: ' . $this->url_controller . '<br>';
//echo 'Action: ' . $this->url_action . '<br>';
//echo 'Parameters: ' . print_r($this->url_params, true) . '<br>';
}
}
}