Beispiel #1
0
 /**
  * Finds and loads a Controller for the given http request
  * @return boolean
  */
 public function dispatch()
 {
     include_once BOOTSTRAP_FILE;
     $this->_domain = $_SERVER['HTTP_HOST'];
     $this->_url = $_SERVER['REQUEST_URI'];
     $this->_args = $_GET;
     $route = $this->findCustomRoute();
     if ($route) {
         //handle custom route
         if ($route->isRedirect()) {
             RequestHelper::redirect($route->httpStatus, $route->redirectLocation);
         } else {
             include CONTROLLER_PATH . $route->controller . '/controller.php';
             $template = $route->controller . '/view';
             $controller = new page();
             $controller->set_view($template);
             $controller->set_args($route->pathVariables);
             $controller->get();
         }
     } else {
         //custom route not found, map url to filesystem
         //auto map subdomains to _subdomain_ folders
         $subdomain = str_replace(Config::get('NAKED_DOMAIN'), '', $this->_domain);
         $subdomainFolder = "";
         if (substr($subdomain, -1) == '.') {
             $subdomain = substr($subdomain, 0, -1);
         }
         if ($subdomain != Config::get('DEFAULT_SUBDOMAIN')) {
             //redirect naked domain to default subdomain, if necessary;
             if (empty($subdomain)) {
                 RequestHelper::redirect(UrlHelper::uri($this->_url));
                 die;
             } else {
                 $subdomainFolder = '_' . $subdomain . '_/';
                 if (!is_dir(CONTROLLER_PATH . $subdomainFolder)) {
                     if (is_dir(CONTROLLER_PATH . '_*_/')) {
                         include CONTROLLER_PATH . '_*_/controller.php';
                         $template = CONTROLLER_PATH . '_*_/view';
                         $controller = new page();
                         $controller->set_view($template);
                         $controller->set_args(array('subdomain' => $subdomain));
                         $controller->get();
                         die;
                     } else {
                         $this->throwError('404');
                     }
                 }
             }
         }
         //on default subdomain, so continue
         $path = UrlHelper::convertUrlToPath($this->_url);
         if (empty($path)) {
             if (is_dir(CONTROLLER_PATH . $subdomainFolder) && is_readable(CONTROLLER_PATH . $subdomainFolder . 'controller.php')) {
                 include CONTROLLER_PATH . $subdomainFolder . 'controller.php';
                 $template = $subdomainFolder . 'view';
             } else {
                 $this->throwError('404');
             }
         } elseif (is_dir(CONTROLLER_PATH . $subdomainFolder . $path) && is_readable(CONTROLLER_PATH . $subdomainFolder . $path . '/controller.php')) {
             include CONTROLLER_PATH . $subdomainFolder . $path . '/controller.php';
             $template = $subdomainFolder . $path . DS . 'view';
         } elseif (is_readable(CONTROLLER_PATH . $subdomainFolder . $path)) {
             $file = CONTROLLER_PATH . $subdomainFolder . $path;
             if (function_exists('finfo_open')) {
                 $finfo = finfo_open(FILEINFO_MIME_TYPE);
                 // return mime type ala mimetype extension
                 header("Content-type: " . finfo_file($finfo, $file));
                 finfo_close($finfo);
             } elseif (function_exists('mime_content_type')) {
                 header("Content-type: " . mime_content_type($file));
             } else {
                 header("Content-type: text/plain");
             }
             readfile($file);
             die;
         } else {
             $this->throwError('404');
             return false;
         }
         $controller = new page();
         $controller->set_view($template);
         $controller->get();
     }
     return true;
 }