/** * Starts the Application * Takes the parts of the URL and loads the according controller & method and passes the parameter arguments to it * TODO: get rid of deep if/else nesting * TODO: make the hardcoded locations ("error/index", "index.php", new Index()) dynamic, maybe via config.php */ public function __construct() { $this->splitUrl(); // check for controller: is the url_controller NOT empty ? if ($this->url_controller) { // check for controller: does such a controller exist ? if (file_exists(CONTROLLER_PATH . $this->url_controller . '.php')) { // 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 CONTROLLER_PATH . $this->url_controller . '.php'; $this->url_controller = new $this->url_controller(); // check for method: does such a method exist in the controller ? if ($this->url_action) { if (method_exists($this->url_controller, $this->url_action)) { // call the method and pass the arguments to it if (isset($this->url_parameter_3)) { $this->url_controller->{$this->url_action}($this->url_parameter_1, $this->url_parameter_2, $this->url_parameter_3); } elseif (isset($this->url_parameter_2)) { $this->url_controller->{$this->url_action}($this->url_parameter_1, $this->url_parameter_2); } elseif (isset($this->url_parameter_1)) { $this->url_controller->{$this->url_action}($this->url_parameter_1); } else { // if no parameters given, just call the method without arguments $this->url_controller->{$this->url_action}(); } } else { // redirect user to error page (there's a controller for that) header('location: ' . URL . 'error/index'); } } else { // default/fallback: call the index() method of a selected controller $this->url_controller->index(); } // obviously mistyped controller name, therefore show 404 } else { // redirect user to error page (there's a controller for that) header('location: ' . URL . 'error/index'); } // if url_controller is empty, simply show the main page (index/index) } else { //redirect for entering page and not logged in if (isset($_SESSION['user_logged_in'])) { require CONTROLLER_PATH . 'home.php'; $controller = new home(); $controller->index(); } else { require CONTROLLER_PATH . 'index.php'; $controller = new index(); $controller->index(); } } }
function __construct() { // //error_reporting(E_ERROR | E_PARSE); $url = isset($_GET['url']) ? $_GET['url'] : null; $url = rtrim($url, '/'); $url = explode('/', $url); //print_r($url); if (empty($url[0])) { require 'Controllers/index.php'; $controller = new index(); $controller->loadModel('index'); $controller->index(); return false; } // $file = 'Controllers/' . $url[0] . '.php'; if (file_exists($file)) { require $file; } else { $this->error(); return false; } $controller = new $url[0](); $controller->loadModel($url[0]); // if (isset($url[2])) { if (method_exists($controller, $url[1])) { $controller->{$url[1]}($url[2]); } else { $this->error(); } } else { if (isset($url[1])) { if (method_exists($controller, $url[1])) { $controller->{$url[1]}(); } else { $this->error(); } } else { $controller->index(); } } }
function __construct() { // Alter the URL to the appropriate MVC call if (!isset($_GET['url'])) { $url[0] = "index"; } else { $url = $_GET['url']; $url = rtrim($url, '/'); $url = explode("/", $url); } $controller_file = CONTROLLERS_DIR . $url[0] . ".php"; if (file_exists($controller_file)) { require $controller_file; $controller = new $url[0](); $controller->loadModel($url[0]); if (isset($url[1]) && method_exists($controller, $url[1])) { // Needs error catching for correct arguments if (isset($url[3])) { $controller->{$url[1]}($url[2], $url[3]); } else { if (isset($url[2])) { $controller->{$url[1]}($url[2]); } else { $controller->{$url[1]}(); } } } else { $controller->index(); } } else { require CONTROLLERS_DIR . "index.php"; $controller = new index(); $controller->index(); return false; } }
public function index_interface() { parent::index(); }