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
|
<?php
/***************************************
* Description: Initial vse komponenete in default
* Autor: Robert Šmalc
* Created date: 22.01.2016
*****************************************/
namespace app;
class Survey
{
/** @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) {
require APP . 'controller/home.php';
$page = new Home();
$page->index();
} elseif (file_exists(APP . 'controller/' . $this->url_controller . '.php')) {
// here we did check for controller: does such a controller exist ?
// if so, then load this file and create this controller
// example: if controller would be "car", then this line would translate into: $this->car = new car();
require APP . 'controller/' . $this->url_controller . '.php';
$this->url_controller = new $this->url_controller();
// check for method: does such a method exist in the controller ?
if (method_exists($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 {
header('location: ' . URL . 'error');
}
}
} else {
header('location: ' . URL . 'error');
}
}
/**
* 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>';
}
}
}
|