/** * Matches a user submitted path with a previously defined route. * Assigns and returns an array of defaults on a successful match. * * @param string $path Path used to match against this routing map * * @return array|false An array of assigned values or a false on a mismatch */ public function route(\Yaf\Request_Abstract $request) { $requestUri = $request->getRequestUri(); $baseuri = $request->getBaseUri(); if ($requestUri != '' && $baseuri != '' && stripos($requestUri, $baseuri) !== false) { $path = substr($requestUri, strlen($baseuri)); } else { $path = $requestUri; } $path = trim(urldecode($path), \Yaf\Router::URI_DELIMITER); $values = $this->_match($path); if ($values == null) { return false; } if (isset($this->_default['module'])) { $request->setModuleName($this->_default['module']); } if (isset($this->_default['controller'])) { $request->setControllerName($this->_default['controller']); } if (isset($this->_default['action'])) { $request->setActionName($this->_default['action']); } $request->setParam($values); return true; }
/** * Matches a user submitted path with a previously defined route. * Assigns and returns an array of defaults on a successful match. * * @param string $path Path used to match against this routing map * @return array|false An array of assigned values or a false on a mismatch */ public function route(\Yaf\Request_Abstract $request) { $requestUri = $request->getRequestUri(); $baseuri = $request->getBaseUri(); if ($requestUri != '' && $baseuri != '' && stripos($requestUri, $baseuri) !== false) { $path = substr($requestUri, strlen($baseuri)); } else { $path = $requestUri; } $path = urldecode($path); $res = preg_match($this->_route, $path, $values); if ($res === 0) { return false; } $values = $this->_getMappedValues($values); if (isset($this->_default['module'])) { $request->setModuleName($this->_default['module']); } if (isset($this->_default['controller'])) { $request->setControllerName($this->_default['controller']); } if (isset($this->_default['action'])) { $request->setActionName($this->_default['action']); } $request->setParam($values); return true; }
/** * <p><b>\Yaf\Route_Interface::route()</b> is the only method that a custom route should implement.</p><br/> * <p>if this method return TRUE, then the route process will be end. otherwise,\Yaf\Router will call next route in the route stack to route request.</p><br/> * <p>This method would set the route result to the parameter request, by calling \Yaf\Request_Abstract::setControllerName(), \Yaf\Request_Abstract::setActionName() and \Yaf\Request_Abstract::setModuleName().</p><br/> * <p>This method should also call \Yaf\Request_Abstract::setRouted() to make the request routed at last.</p> * * @link http://www.php.net/manual/en/yaf-route-interface.route.php * * @param \Yaf\Request_Abstract $request * @desc 默认module和controller均写死为Index了 * @return bool */ public function route($request) { $uris = explode('/', str_replace('-', '', $request->getRequestUri())); $request->setModuleName('Index'); if (!empty($uris[1])) { $request->setControllerName($uris[1]); } else { $request->setControllerName('Index'); } $params = array(); $param_pairs = array_chunk(array_slice($uris, 2), 2); foreach ($param_pairs as $param) { $params[$param[0]] = empty($param[1]) ? '' : $param[1]; } $request->setParam($params); $request->setParam($_REQUEST); $request->setActionName(strtolower($request->getMethod())); $method = $request->getParam('_method'); if (!empty($method)) { $request->setActionName(strtolower($method)); } //如果不return true 会继续执行static 路由规则 return true; }
/** * Processes a request and sets its controller and action. If * no route was possible, default route is set. * * @param Yaf_Request_Abstract * @return Yaf_Request_Abstract|boolean */ public function route(\Yaf\Request_Abstract $request) { $requestUri = $request->getRequestUri(); $baseuri = $request->getBaseUri(); if ($requestUri != '' && $baseuri != '' && stripos($requestUri, $baseuri) === 0) { $path = substr($requestUri, strlen($baseuri)); } else { $path = $requestUri; } $path = trim(urldecode($path), \Yaf\Router::URI_DELIMITER); $rest = ''; if (is_string($this->_delimiter) && $this->_delimiter != '') { if (($queryStringPos = strpos($path, $this->_delimiter)) !== false) { $rest = substr($path, $queryStringPos + strlen($this->_delimiter), strlen($path) - 1); $path = substr($path, 0, $queryStringPos); } } $route = ''; if ($path != '' && $path != '/') { $route = str_replace(\Yaf\Router::URI_DELIMITER, '_', trim($path, \Yaf\Router::URI_DELIMITER)); } if ($route != '') { if ($this->_ctlPrefer == true) { $request->setControllerName($route); } else { $request->setActionName($route); } } $params = array(); if ($rest != null && trim($rest) != '') { $path = explode(\Yaf\Router::URI_DELIMITER, trim($rest, \Yaf\Router::URI_DELIMITER)); if (($numSegs = count($path)) != 0) { for ($i = 0; $i < $numSegs; $i = $i + 2) { $key = urldecode($path[$i]); $val = isset($path[$i + 1]) ? urldecode($path[$i + 1]) : null; $params[$key] = isset($params[$key]) ? array_merge((array) $params[$key], array($val)) : $val; } } $request->setParam($params); } return true; }
/** * Processes a request and sets its controller and action based on a * supervar value. * * @param Yaf_Request_Abstract * @return Yaf_Request_Abstract|boolean */ public function route(\Yaf\Request_Abstract $request) { $requestUri = $request->getQuery($this->_varName); if ($requestUri == null || $requestUri == '') { return false; } $module = null; $controller = null; $action = null; $rest = null; $path = trim($requestUri, \Yaf\Router::URI_DELIMITER); if ($path != '' && $path != '/') { $path = explode(\Yaf\Router::URI_DELIMITER, $path); if (\Yaf\Application::isModuleName($path[0])) { $module = $path[0]; array_shift($path); } if (count($path) && !empty($path[0])) { $controller = $path[0]; array_shift($path); } if (count($path) && !empty($path[0])) { $action = $path[0]; array_shift($path); } $rest = implode(\Yaf\Router::URI_DELIMITER, $path); $actionPrefer = \Yaf\G::iniGet('yaf.action_prefer'); if ($module == null && $controller == null && $action == null) { if ($actionPrefer == true) { $action = $rest; } else { $controller = $rest; } $rest = null; } elseif ($module == null && $action == null && $rest == null) { if ($actionPrefer == true) { $action = $controller; $controller = null; } } elseif ($controller == null && $action == null && $rest != null) { $controller = $module; $action = $rest; $module = null; $rest = null; } elseif ($action == null && $rest == null) { $action = $controller; $controller = $module; $module = null; } elseif ($controller == null && $action == null) { $controller = $module; $action = $rest; $module = null; $rest = null; } elseif ($action == null) { $action = $rest; $rest = null; } if ($module != null) { $request->setModuleName($module); } if ($controller != null) { $request->setControllerName($controller); } if ($action != null) { $request->setActionName($action); } $params = array(); if ($rest != null && trim($rest) != '') { $path = explode(\Yaf\Router::URI_DELIMITER, $rest); if (($numSegs = count($path)) != 0) { for ($i = 0; $i < $numSegs; $i = $i + 2) { $key = urldecode($path[$i]); $val = isset($path[$i + 1]) ? urldecode($path[$i + 1]) : null; $params[$key] = isset($params[$key]) ? array_merge((array) $params[$key], array($val)) : $val; } } $request->setParam($params); } } return true; }