/**
  * @throws \Exception
  */
 public function dispatch()
 {
     if ($this->router == null) {
         //TODO
         throw new \Exception('No valid router found', 500);
     }
     $_uri = $this->router->getURI();
     $routes = \GF\App::getInstance()->getConfig()->routes;
     $rc = null;
     if (is_array($routes) && count($routes) > 0) {
         foreach ($routes as $key => $val) {
             if (stripos($_uri, $key) === 0 && ($_uri == $key || stripos($_uri, $key . '/') === 0) && $val['namespace']) {
                 $this->namespace = $val['namespace'];
                 $_uri = substr($_uri, strlen($key) + 1);
                 $rc = $val;
                 break;
             }
         }
     } else {
         //TODO
         throw new \Exception('Default route missing', 500);
     }
     if ($this->namespace == null && $routes['*']['namespace']) {
         $this->namespace = $routes['*']['namespace'];
         $rc = $routes['*'];
     } else {
         if ($this->namespace == null && !$routes['*']['namespace']) {
             //TODO
             throw new \Exception('Default router is missing', 500);
         }
     }
     $input = \GF\InputData::getInstance();
     $params = explode('/', $_uri);
     if ($params[0]) {
         $this->controller = strtolower($params[0]);
         if ($params[1]) {
             $this->method = strtolower($params[1]);
             uset($params[0], $params[1]);
             $input->setGet(array_values($params));
         } else {
             $this->method = $this->getDefaultMethod();
         }
     } else {
         $this->controller = $this->getDefaultController();
         $this->method = $this->getDefaultMethod();
     }
     if (is_array($rc) && $rc['controllers']) {
         if ($rc['controllers'][$this->controller]['method'][$this->method]) {
             $this->method = strtolower($rc['controllers'][$this->controller]['method'][$this->method]);
         }
         if (isset($rc['controllers'][$this->controller]['to'])) {
             $this->controller = strtolower($rc['controllers'][$this->controller]['to']);
         }
     }
     $input->setPost($this->router->getPost());
     //TODO Fix it
     $f = $this->namespace . '\\' . ucfirst($this->controller);
     $newController = new $f();
     $newController->{$this->method}();
 }
 public function __construct()
 {
     $this->app = \GF\App::getInstance();
     $this->view = \GF\View::getInstance();
     $this->config = $this->app->getConfig();
     $this->input = \GF\InputData::getInstance();
 }
示例#3
0
 public function dispatch()
 {
     //        echo 'URI: ' . $this->router->getURI();
     if ($this->router == null) {
         throw new \Exception('No valid router found', 500);
     }
     $_uri = $this->router->getURI();
     $routes = \GF\App::getInstance()->getConfig()->routes;
     $_rc = null;
     if (is_array($routes) && count($routes) > 0) {
         foreach ($routes as $k => $v) {
             if (stripos($_uri, $k) === 0 && ($_uri == $k || stripos($_uri, $k . '/') === 0) && $v['namespace']) {
                 $this->ns = $v['namespace'];
                 $_uri = substr($_uri, strlen($k) + 1);
                 $_rc = $v;
                 break;
             }
         }
     } else {
         throw new \Exception('Default route missing', 500);
     }
     if ($this->ns == null && $routes['*']['namespace']) {
         $this->ns = $routes['*']['namespace'];
         $_rc = $routes['*'];
     } else {
         if ($this->ns == null && !$routes['*']['namespace']) {
             throw new \Exception('Default route missing', 500);
         }
     }
     $input = \GF\InputData::getInstance();
     $_params = explode('/', $_uri);
     if ($_params[0]) {
         $link = $_params[0];
         $db = new \GF\DB\SimpleDB();
         $db->prepare("SELECT link FROM blogs WHERE link=?", array($link))->execute()->fetchAllAssoc();
         if ($db->getAffectedRows() == 1) {
             if ($_params[1]) {
                 $this->controller = strtolower($_params[1]);
             } else {
                 $this->controller = 'blogindex';
             }
             //if we do not have controller and method, we do not have params
             if ($_params[2]) {
                 $this->method = strtolower($_params[2]);
                 unset($_params[0], $_params[1], $_params[2]);
                 $input->setGet(array_values($_params));
             } else {
                 $this->method = $this->getDefaultMethod();
             }
         } else {
             $this->controller = strtolower($_params[0]);
             //if we do not have controller and method, we do not have params
             if ($_params[1]) {
                 $this->method = strtolower($_params[1]);
                 unset($_params[0], $_params[1]);
                 $input->setGet(array_values($_params));
             } else {
                 $this->method = $this->getDefaultMethod();
             }
         }
     } else {
         $this->controller = $this->getDefaultController();
         $this->method = $this->getDefaultMethod();
     }
     if (is_array($_rc) && $_rc['controllers']) {
         if ($_rc['controllers'][$this->controller]['methods'][$this->method]) {
             $this->method = strtolower($_rc['controllers'][$this->controller]['methods'][$this->method]['to']);
         }
         if (isset($_rc['controllers'][$this->controller]['to'])) {
             $this->controller = strtolower($_rc['controllers'][$this->controller]['to']);
         }
     }
     //        echo '<br />N: ' . $this->ns;
     //        echo '<br />C: ' . ucfirst($this->controller);
     //        echo '<br />M: ' . $this->method . '<br />';
     $input->setPost($this->router->getPost());
     //TODO fix it
     $f = $this->ns . '\\' . ucfirst($this->controller);
     $newControoler = new $f();
     $newControoler->{$this->method}();
 }