コード例 #1
0
ファイル: Regex.php プロジェクト: lfq618/yafdemo
 /**
  * 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;
 }
コード例 #2
0
 /**
  * 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;
 }
コード例 #3
0
ファイル: Router.php プロジェクト: pancke/yyaf
 /**
  * Find a matching route to the current Request and inject
  * returning values to the Request object.
  *
  * @return bool if there is a valid route
  */
 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;
     }
     //支持rewrite路由
     $matches = null;
     $rewrites = Yaf_G::getConf('rewrite', 'route');
     $rest = explode('?', $path, 2);
     $path = $rest[0];
     if (!empty($rewrites)) {
         foreach ($rewrites as $k => $v) {
             $matches = null;
             if (preg_match($k, $path, $matches)) {
                 $path = preg_replace($k, $v, $path);
                 break;
             }
         }
     }
     if (!empty($rest[1])) {
         $path .= '?' . $rest[1];
     }
     //取得Route
     $aRoute = Yaf_G::getRoute($path);
     $request->setModuleName($aRoute['module']);
     $request->setControllerName($aRoute['controller']);
     $request->setActionName($aRoute['action']);
     //解析参数
     parse_str($aRoute['query'], $params);
     /*
     $params = array();
     $rest = $aRoute['query'];
     $numSegs = count($rest);
     if ($numSegs > 0) {
         for ($i = 0; $i < $numSegs; $i = $i + 2) {
             $key = $rest[$i];
             $val = isset($rest[$i + 1]) ? $rest[$i + 1]: null;
             $params[$key] = (isset($params[$key]) ? (array_merge((array) $params[$key], array(
                 $val
             ))) : $val);
         }
     }
     */
     //rewrite参数解析
     if (!empty($matches)) {
         foreach ($matches as $k => $v) {
             if (!is_numeric($k)) {
                 $params[$k] = $v;
             }
         }
     }
     $request->setParam($params);
     return true;
 }
コード例 #4
0
ファイル: Map.php プロジェクト: zhangjingpu/yaf-lib
 /**
  * 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;
 }
コード例 #5
0
 /**
  * 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;
     }
     $module = null;
     $controller = null;
     $action = null;
     $rest = null;
     $path = trim($path, Yaf_Router::URI_DELIMITER);
     if ($path != '') {
         $path = explode(Yaf_Router::URI_DELIMITER, $path);
         $path = array_filter($path, 'strlen');
         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;
 }