Beispiel #1
0
 /**
  * "Start" the application:
  * Analyze the URL elements and calls the according controller/method or the fallback
  */
 public function __construct()
 {
     // create array with URL parts in $url
     $this->splitUrl();
     // check for controller: no controller given ? then load start-page
     if (!$this->controllerName) {
         require CONTROLLER_PATH . 'home_ctrl.php';
         $page = new HomeCtrl();
         $page->index();
     } elseif ($this->controllerName == 'public') {
         return;
     } elseif (!file_exists(CONTROLLER_PATH . $this->controllerName . '_ctrl.php')) {
         require CONTROLLER_PATH . 'error_ctrl.php';
         $page = new ErrorCtrl();
         $page->notFound();
     } else {
         // here we did check for controller: does such a controller exist ?
         // 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();
         $controllerFile = $this->controllerName . '_ctrl.php';
         $controllerName = $this->controllerName . 'Ctrl';
         require CONTROLLER_PATH . $controllerFile;
         $this->controller = new $controllerName();
         // check for method: does such a method exist in the controller ?
         if (!method_exists($this->controller, $this->actionName)) {
             if (strlen($this->actionName) == 0) {
                 // no action defined: call the default index() method of a selected controller
                 $this->controller->index();
             } else {
                 require CONTROLLER_PATH . 'error_ctrl.php';
                 $page = new ErrorCtrl();
                 $page->notFound();
             }
         } else {
             $reflectionMethod = new \ReflectionMethod($this->controller, $this->actionName);
             $numberOfRequiredParameters = $reflectionMethod->getNumberOfRequiredParameters();
             if (sizeof($this->params) < $numberOfRequiredParameters) {
                 require CONTROLLER_PATH . 'error_ctrl.php';
                 $page = new ErrorCtrl();
                 $page->notFound();
             } else {
                 if (!empty($this->params)) {
                     // Call the method and pass arguments to it
                     call_user_func_array(array($this->controller, $this->actionName), $this->params);
                 } else {
                     // If no parameters are given, just call the method without parameters, like $this->home->method();
                     $this->controller->{$this->actionName}();
                 }
             }
         }
     }
 }
Beispiel #2
0
    switch ($controller) {
        case '':
        case 'home':
            require_once 'controllers/HomeCtrl.php';
            $controller = new HomeCtrl();
            break;
        case 'clothes':
            require_once 'controllers/ClothesCtrl.php';
            $controller = new ClothesCtrl();
            break;
        case 'contact':
            require_once 'controllers/ContactCtrl.php';
            $controller = new ContactCtrl();
            break;
        case 'shopping-cart':
            require_once 'controllers/ShoppingCartCtrl.php';
            $controller = new ShoppingCartCtrl();
            break;
        case 'UserCtrl':
            require_once 'controllers/UserCtrl.php';
            $controller = new UserCtrl();
            break;
        default:
            $this->showError();
            break;
    }
} else {
    require_once 'controllers/HomeCtrl.php';
    $controller = new HomeCtrl();
}
$controller->execute();