Beispiel #1
0
 /**
  * find controller and call action
  **/
 public static function callAction($controller, $action)
 {
     if (!$controller) {
         $controller = 'home';
     }
     if (!$action) {
         $action = 'index';
     }
     self::$controller = $controller;
     self::$action = $action;
     //set content type
     if (!headers_sent()) {
         header('Content-type: text/html; charset=utf-8');
     }
     if (strpos($controller, '_') === false) {
         //such as 'user/action'
         $controller = ucfirst($controller);
     } else {
         //such as 'user_extra/action'
         $controllerArr = explode('_', $controller);
         $controllerArr = array_map('ucfirst', $controllerArr);
         $controller = implode('_', $controllerArr);
     }
     if (!self::controllerExists($controller)) {
         self::_pageNotFound();
     } else {
         $classname = 'Controller_' . $controller;
         $klass = new $classname();
         $methods = get_class_methods($klass);
         if (!in_array($action . 'Action', $methods)) {
             //action not found
             self::_pageNotFound();
         } else {
             //action find
             try {
                 //call 'before' function
                 $klass->before();
                 //call action
                 $do = $action . 'Action';
                 $klass->{$do}();
                 //call 'after' function
                 $klass->after();
             } catch (Exception $e) {
                 self::_internalServerError($e);
             }
         }
     }
 }