/** * calls a command from a url * allows a command to be call with complete rules in tact * * @param string|boolean $uri -- the url to be used for the request. Defaults to false * @param array $data -- the data to be passed with the request. This mimics a GET or POST * @param boolean $log -- flag to log the current requested url in user's session. Defaults to false * @access public * @static * @return mixed */ public static function url($uri = false, $data = array(), $log = false) { if ($uri) { $url_obj = System_Request_Url::getInstance()->setRequestUri($uri); } $objects = System_Request_Router::getInstance()->matchUrl(); $request = new System_Request_Controller($objects); if (count($data)) { $request->setCommandData($data); } /** * log the request in the user's session */ if ($log) { $uri = implode('/', System_Request_Url::getRequestVars()); if (!in_array($uri, System_Request_Router::getIgnoredRoutes())) { $_SESSION['requested_uri'] = '/' . $uri; } } return $request->setUp()->process(); }
/** * the url vars for a request is passed and matched against the keys in the defined_routes property * if found, the array containing the command, view, and model objects is returned * * @access public * @return array */ public function matchUrl() { $return = array(); $url_obj = System_Request_Url::getInstance(); $url_parts = $url_obj->getRequestVars(); $request_string = implode('/', $url_parts); $request_string = trim($request_string) == '' ? self::DEFAULT_COMMAND_OBJECT : $request_string; $rules = array(); /** * Hack used to reorder the keys by length * This should be moved to an array lib for reuse * TODO: find another way to match a url against the a defined route */ uksort($this->_routes, array('System_Request_Router', 'keyLengthSort')); foreach ($this->_routes as $url => $route_rules) { if (preg_match_all('/^' . preg_quote($url, '/') . '/i', $request_string, $matches)) { $return = self::getDefaultSetup(); foreach ($route_rules as $key => $val) { $return[$key] = $val; } $rules = $route_rules; if ($url === $request_string) { break; } } } if (isset($rules['rewrites']) && count($rules['rewrites'])) { $url_obj->rewriteKeys($rules['rewrites'], $rules['defaults']); } return $return; }