From 1a89be51a7bf0023003e9180040c6a36dfa80379 Mon Sep 17 00:00:00 2001 From: Mark Hewitt Date: Thu, 17 May 2018 15:47:54 -0400 Subject: [PATCH 1/2] Enabled HomeController additional methods If the controller for a specified path doesn't exist, then it will search the HomeController.php for the method. If the method exists in HomeController, it fires it; if the method doesn't exist, it shows the error message. --- application/Core/Application.php | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/application/Core/Application.php b/application/Core/Application.php index 083674c..bb25c7b 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; @@ -58,8 +61,17 @@ public function __construct() } } } 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 { + $page = new \Mini\Controller\ErrorController(); + $page->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 . '
'; From d9f6e24afc357b90a7cbb887b0fc275710f22a76 Mon Sep 17 00:00:00 2001 From: Mark Hewitt Date: Thu, 17 May 2018 15:55:39 -0400 Subject: [PATCH 2/2] Changed $page to $this->url_controller for error pages and home page $this->url_controller was probably kept away from error pages before so that a redirect could happen. With the new $this->url, that is unnecessary, and adds inconsistency the code. This should fix that. --- application/Core/Application.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/application/Core/Application.php b/application/Core/Application.php index bb25c7b..6ec5d19 100644 --- a/application/Core/Application.php +++ b/application/Core/Application.php @@ -28,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 ? @@ -56,8 +56,8 @@ 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 { @@ -69,8 +69,8 @@ public function __construct() call_user_func_array(array($this->url_controller, $this->url[0]), $this->url_params); } } else { - $page = new \Mini\Controller\ErrorController(); - $page->index(); + $this->url_controller = new \Mini\Controller\ErrorController(); + $this->url_controller->index(); } } }