Exemplo n.º 1
0
 /**
  * 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 . 'login.php';
             $controller = new login();
             $controller->index();
         } else {
             //redirect for entering page and logged in still
             require CONTROLLER_PATH . 'home.php';
             $controller = new home();
             $controller->index();
         }
     }
 }
Exemplo n.º 2
0
 function __construct()
 {
     $url = isset($_GET['url']) ? $_GET['url'] : null;
     $url = rtrim($url, '/');
     $url = explode('/', $url);
     if (empty($url[0])) {
         require 'controller/home.php';
         $controller = new home();
         $controller->index();
         return false;
     }
     $file = 'controller/' . $url[0] . '.php';
     if (file_exists($file)) {
         require $file;
     } else {
         $this->error();
     }
     $controller = new $url[0]();
     $controller->loadModel($url[0]);
     // Calling methods
     $method = "{$url['1']}(";
     for ($i = 2; $i <= count($url); $i++) {
         if ($i != count($url)) {
             if ($i > 2) {
                 $method .= ",";
             }
             $method .= "'{$url[$i]}'";
         }
     }
     $method .= ");";
     if (isset($url[2])) {
         if (method_exists($controller, $url[1])) {
             $ads = '$controller->' . $method . '';
             eval($ads);
         } else {
             $this->error();
         }
     } else {
         if (isset($url[1])) {
             if (method_exists($controller, $url[1])) {
                 $controller->{$url[1]}();
             } else {
                 $this->error();
             }
         } else {
             $controller->index();
         }
     }
 }
Exemplo n.º 3
0
 function __construct()
 {
     $url = $this->parseUrl();
     //set home as our default controller
     $file = SITE_PATH . '/app/controllers/home_controller.php';
     //if no  url type then load home
     if (empty($url[0])) {
         require_once $file;
         $control = new home();
         $control->index();
         return FALSE;
     }
     //if controller not empty then check if it is a valid conroller
     $file = SITE_PATH . '/app/controllers/' . $url[0] . '_controller.php';
     //if valid then include it
     if (file_exists($file)) {
         require_once $file;
     } else {
         return miscellaneous::Error();
     }
     $name = $url[0];
     //create an object of that controller
     $control = new $name();
     //params array save the method and parameter of the controller class
     $params = [];
     //by default we set the methos as index
     $method = 'index';
     //if method is set in url
     if (isset($url[1])) {
         //if it is index then skip it and show error
         if ($url[1] === 'index') {
             return miscellaneous::Error();
         } else {
             if (method_exists($url[0], $url[1])) {
                 //if method exists then set the method
                 $method = $url[1];
                 unset($url[0]);
                 unset($url[1]);
                 $params = $url ? array_values($url) : [];
             } else {
                 return miscellaneous::Error();
             }
         }
     }
     //load the model required
     $control->loadModel($name);
     //finally call the method with paramters of that controller class
     call_user_func_array([$control, $method], $params);
 }