Parcourir la source

Update: route parse update

Zhu Jinhui il y a 7 ans
Parent
commit
fa5a0c3595
3 fichiers modifiés avec 41 ajouts et 12 suppressions
  1. 13 8
      src/Base/Request.php
  2. 16 2
      src/Request/Url/Base.php
  3. 12 2
      src/Router/Parse/Normal.php

+ 13 - 8
src/Base/Request.php

@@ -47,16 +47,21 @@ abstract class Request
         $this->host = IS_CLI ? '' : $_SERVER['HTTP_HOST'];
         $params = (array)$this->url->getParams();
         if(count($params) > 0) $this->params = array_merge($this->params, $params);
+        $routeInfo = $this->url->getPathArgs();
+		$controller = $this->defaultController();
+		$action = $this->defaultAction();
+		if(count($routeInfo) > 1)
+		{
+			$action = array_pop($routeInfo);
+			$controller = join("\\", $routeInfo);
+		}
+		else if(count($routeInfo) == 1 && !empty($routeInfo[0])) {
+			$controller = $routeInfo[0];
+		}
         //处理url中的数据
         if(ucwords($rewriteRule) == 'Short'){
-            $this->setControllerName(
-                    isset($this->params[0]) && $this->params[0] != '' ?
-                        $this->params[0] :
-                        $this->defaultController());
-            $this->setActionName(
-                    isset($this->params[1]) && $this->params[1] != '' ?
-                        $this->params[1] :
-                        $this->defaultAction());
+            $this->setControllerName($controller);
+            $this->setActionName($action);
         }
         return $this;
     }

+ 16 - 2
src/Request/Url/Base.php

@@ -39,6 +39,15 @@ abstract class Base
      * URL中匹配到的参数
      */
     private $params = null;
+	
+	/**
+	 * 文件扩展名
+	 */
+	private $sysfileExtension;
+	/**
+	 * 存储路径字段
+	 */
+	private $pathArgs;
 
     /**
      * 初始化模式
@@ -299,15 +308,20 @@ abstract class Base
         $extenstion[2] = str_replace('/', '\/', $extenstion[2]);
         $query = preg_replace("/\.{$extenstion[2]}$/", "", $query);
         $paramArray = explode($this->_symbol, $query);
-        $v = $this->decodeArgs($paramArray);
+        $this->pathArgs = $v = $this->decodeArgs($paramArray);
         //添加系统扩展名到返回数组中 2011-10-14 15:26
-        $v['sysfileExtension'] = $extenstion[2];
+		$this->sysfileExtension = $extension[2];
         if ($_GET) $v = array_merge($v, $_GET);
         if ($key != '' || is_int($key)) {
             return $v[$key];
         }
         return $v;
     }
+	
+	public function getPathArgs()
+	{
+		return $this->pathArgs;
+	}
 
     /**
      * 对比转发文件的路径

+ 12 - 2
src/Router/Parse/Normal.php

@@ -110,8 +110,18 @@ class Normal
             $match['controller'] = $controller;
             $match['action'] = $action;
         } else {
-            $match['controller'] = isset($dirInfo[0]) ? $dirInfo[0] : 'index';
-            $match['action'] = isset($dirInfo[1]) ? $dirInfo[1] : 'index';
+			$controller = 'index';
+			$action = 'index';
+			if(count($dirInfo) > 1)
+			{
+				$action = array_pop($dirInfo);
+				$controller = join('\\', $dirInfo);
+			}
+			else if(count($dirInfo) == 1 && !empty($dirInfo[0])) {
+				$controller = $dirInfo[0];
+			}
+            $match['controller'] = $controller;
+            $match['action'] = $action;
             //匹配配置文件中以 * 开头的规则
             foreach($this->config as $key => $config)
             {