/** * Parses the URI for this page and finds the appropriate controller to run. * This is the last thing that should happen in the index and should never be * called again. * * This is called automatically by the index.php script. */ public function run($uri) { $this->uri = $uri; $uriParts = $this->getURISplit(); $name = null; $function = null; $params = array(); foreach ($uriParts as $part) { if ($part !== '') { $part = urldecode($part); if ($name == null) { $name = $part; } else { if ($function == null) { $function = $part; } else { $params[] = $part; } } } } // check we have a name and it's not for this script if ($name == null || $rootURI . $name == $_SERVER['PHP_SELF']) { $name = $this->defaultController; } // ignore functions beginning with an underscore if (strpos($function, '_') === 0) { $function = null; } if ($function == null) { $function = $this->defaultFunction; } $controllerPath = $this->findController($name); if ($controllerPath === false) { $name = $this->defaultController; $controllerPath = $this->findController($name); } if ($controllerPath === false) { throw new Exception('Default controller not found: ' . $this->defaultController); } else { require $controllerPath; if (class_exists($name)) { Flexi::pushFlexi($this); $controller = new $name(); if (!method_exists($controller, $function)) { $function = $this->defaultFunction; } $frame = null; if ($this->frames != null) { $frame = $this->frames->getFrame($name, $function); $controller->__setFrame($frame); } if (!$this->tryControllerMethod($controller, $name, $function, $params)) { Flexi::popFlexi(); throw new Exception('Default method \'' . $function . '\' not found in controller: ' . $name . '\'.'); } else { if ($frame !== null) { $frame->_runToEnd(); Flexi::popFlexi(); } } } else { Flexi::popFlexi(); throw new Exception('Controller loaded but class not found: ' . $name); } } }