Beispiel #1
0
 /**
  * 开始路由匹配
  * @author Xiao Tangren  <*****@*****.**>
  * @data 2014-03-05
  * @param UnPHP_Request_Abstract $request
  */
 public function route(UnPHP_Request_Abstract $request)
 {
     $rs = FALSE;
     $base_url = $request->getServer('REQUEST_URI');
     $selfPramas = stripos($base_url, '?');
     $base_url = $selfPramas ? substr($base_url, 0, $selfPramas) : $base_url;
     if ($base_url == '/') {
         $request->setModuleName($request->getDefaultModule());
         $request->setControllerName($request->getDefaultController());
         $request->setActionName($request->getDefaultAction());
         $rs = TRUE;
     }
     $this->_routes['default'] = new UnPHP_Route_Default();
     if (FALSE === $rs && !empty($this->_routes)) {
         // 遍历理由协议
         foreach ($this->_routes as $name => $route) {
             if ($route->route($request)) {
                 $this->_current_route = $name;
                 $rs = TRUE;
                 break;
             }
         }
     }
     return $rs;
 }
 public function route(UnPHP_Request_Abstract $request)
 {
     $rs = false;
     $m = $request->getQuery($this->m);
     $c = $request->getQuery($this->c);
     $a = $request->getQuery($this->a);
     $c = !empty($m) && empty($c) ? $request->getDefaultController() : $c;
     $a = !empty($m) && !empty($c) && empty($a) ? $request->getDefaultAction() : $a;
     if (!empty($m) && !empty($c) && !empty($a)) {
         $rs = true;
         $request->setModuleName($m);
         $request->setControllerName($c);
         $request->setActionName($a);
     }
     return $rs;
 }
 /**
  * 分发之前
  * 如果在一个请求处理过程中, 发生了forward, 则这个事件会被触发多次
  * @author xiaotangren  <*****@*****.**>
  */
 public function preDispatch(UnPHP_Request_Abstract $request, UnPHP_Response_Abstract $response)
 {
     //$this->initModule($module_name, $request, $response);
     if (Pub_Url::$custom_route) {
         $data = Pub_Url::$custom_route;
         if ($data['type'] == 'p') {
             $request->module = 'Goods';
             $request->controller = 'Index';
             $request->action = 'index';
             $request->method = 'GET';
             $request->setParam('id', $data['id']);
         } else {
             $request->module = 'Category';
             $request->controller = 'Index';
             $request->action = 'index';
             $request->method = 'GET';
             $request->setParam('ocid', $data['id']);
         }
     }
     $module_name = $request->getModuleName();
     //var_dump($module_name);exit;
     $this->initModule($module_name, $request, $response);
 }
 /**
  * 默认路由: /m/c/a/params1/1/params2/2/...
  * @author Xiao Tangren  <*****@*****.**>
  * @data 2014-03-05
  * @param type $request
  * @return boolean
  */
 public function route(UnPHP_Request_Abstract $request)
 {
     $rs = FALSE;
     $match = array();
     $base_url = $request->getServer('REQUEST_URI');
     $selfPramas = stripos($base_url, '?');
     $base_url = $selfPramas ? substr($base_url, 0, $selfPramas) : $base_url;
     $match = explode("/", trim($base_url, '/\\'));
     $n = count($match);
     switch ($n) {
         case 0:
             $request->setModuleName($request->getDefaultModule());
             $request->setControllerName($request->getDefaultController());
             $request->setActionName($request->getDefaultAction());
             $rs = TRUE;
             break;
         case 1:
             if (in_array($match[0], UnPHP::app()->getModules())) {
                 $request->setModuleName($match[0]);
                 $request->setControllerName($request->getDefaultController());
                 $request->setActionName($request->getDefaultAction());
             } else {
                 $request->setModuleName($request->getDefaultModule());
                 $request->setControllerName($match[0]);
                 $request->setActionName($request->getDefaultAction());
             }
             $rs = TRUE;
             break;
         case 2:
             if (in_array($match[0], UnPHP::app()->getModules())) {
                 $request->setModuleName($match[0]);
                 $request->setControllerName($match[1]);
                 $request->setActionName($request->getDefaultAction());
                 $rs = TRUE;
                 break;
             } else {
                 $request->setModuleName($request->getDefaultModule());
                 $request->setControllerName($match[0]);
                 $request->setActionName($match[1]);
                 $rs = TRUE;
                 break;
             }
         default:
             if (in_array($match[0], UnPHP::app()->getModules())) {
                 $request->setModuleName($match[0]);
                 $request->setControllerName($match[1]);
                 $request->setActionName($match[2]);
                 for ($i = 4; $i < $n; $i += 2) {
                     $request->setParam($match[$i - 1], $match[$i]);
                 }
             } else {
                 $request->setModuleName($request->getDefaultModule());
                 $request->setControllerName($match[0]);
                 $request->setActionName($match[1]);
                 for ($i = 3; $i < $n; $i += 2) {
                     $request->setParam($match[$i - 1], $match[$i]);
                 }
             }
             $rs = TRUE;
             break;
     }
     return $rs;
 }