diff --git a/application/Core/Application.php b/application/Core/Application.php index 083674c..6ec5d19 100644 --- a/application/Core/Application.php +++ b/application/Core/Application.php @@ -4,6 +4,9 @@ class Application { + /** @var null Parts of the URL, as provided and sanitized by splitURL() */ + private $url = []; + /** @var null The controller */ private $url_controller = null; @@ -25,8 +28,8 @@ public function __construct() // check for controller: no controller given ? then load start-page if (!$this->url_controller) { - $page = new \Mini\Controller\HomeController(); - $page->index(); + $this->url_controller = new \Mini\Controller\HomeController(); + $this->url_controller->index(); } elseif (file_exists(APP . 'Controller/' . ucfirst($this->url_controller) . 'Controller.php')) { // here we did check for controller: does such a controller exist ? @@ -53,13 +56,22 @@ public function __construct() // no action defined: call the default index() method of a selected controller $this->url_controller->index(); } else { - $page = new \Mini\Controller\ErrorController(); - $page->index(); + $this->url_controller = new \Mini\Controller\ErrorController(); + $this->url_controller->index(); } } } else { - $page = new \Mini\Controller\ErrorController(); - $page->index(); + $this->url_controller = new \Mini\Controller\HomeController(); + if (method_exists($this->url_controller, $this->url[0])) { + if (isset($this->url_action)) { + call_user_func_array(array($this->url_controller, $this->url[0]), array_merge([$this->url_action], $this->url_params)); + } else { + call_user_func_array(array($this->url_controller, $this->url[0]), $this->url_params); + } + } else { + $this->url_controller = new \Mini\Controller\ErrorController(); + $this->url_controller->index(); + } } } @@ -73,19 +85,16 @@ private function splitUrl() // split URL $url = trim($_GET['url'], '/'); $url = filter_var($url, FILTER_SANITIZE_URL); - $url = explode('/', $url); + $this->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]); + $this->url_controller = isset($this->url[0]) ? $this->url[0] : null; + $this->url_action = isset($this->url[1]) ? $this->url[1] : null; - // Rebase array keys and store the URL params - $this->url_params = array_values($url); + // Store the URL params + $this->url_params = array_slice($this->url, 2); // for debugging. uncomment this if you have problems with the URL //echo 'Controller: ' . $this->url_controller . '
';