Skip to content

Enabled HomeController additional methods #44

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 23 additions & 14 deletions application/Core/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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 ?
Expand All @@ -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();
}
}
}

Expand All @@ -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 . '<br>';
Expand Down