parsePathInfo() public static method

解析url获取pathinfo
public static parsePathInfo ( ) : void
return void
Example #1
0
 /**
  * 解析url
  *
  * @return mixed
  */
 public function parseUrl()
 {
     $dispatcher = \FastRoute\simpleDispatcher(function (RouteCollector $r) {
         foreach ($this->routes as $route) {
             $r->addRoute($route['method'], $route['uri'], $route['action']);
         }
     });
     \Cml\Route::parsePathInfo();
     $httpMethod = isset($_POST['_method']) ? strtoupper($_POST['_method']) : strtoupper($_SERVER['REQUEST_METHOD']);
     $routeInfo = $dispatcher->dispatch($httpMethod, implode('/', \Cml\Route::getPathInfo()));
     switch ($routeInfo[0]) {
         case Dispatcher::NOT_FOUND:
         case Dispatcher::METHOD_NOT_ALLOWED:
             break;
         case Dispatcher::FOUND:
             $_GET += $routeInfo[2];
             if (is_callable($routeInfo[1])) {
                 call_user_func($routeInfo[1]);
                 Cml::cmlStop();
             }
             $this->parseUrlParams($routeInfo[1]);
             break;
     }
     return $dispatcher;
 }
Example #2
0
 /**
  * 解析url
  *
  * @return void
  */
 public function parseUrl()
 {
     $path = '/';
     //定义URL常量
     $subDir = dirname($_SERVER['SCRIPT_NAME']);
     if ($subDir == '/' || $subDir == '\\') {
         $subDir = '';
     }
     //定义项目根目录地址
     self::$urlParams['root'] = $subDir . '/';
     \Cml\Route::parsePathInfo();
     $pathInfo = \Cml\Route::getPathInfo();
     //检测路由
     if (self::$rules) {
         //配置了路由,所有请求通过路由处理
         $isRoute = self::isRoute($pathInfo);
         if ($isRoute[0]) {
             //匹配路由成功
             if (is_array($isRoute['route'])) {
                 self::$urlParams['action'] = $isRoute['route'][2];
                 self::$urlParams['controller'] = $isRoute['route'][1];
                 $path = self::$urlParams['path'] = $isRoute['route'][0];
             } else {
                 $routeArr = explode('/', $isRoute['route']);
                 $isRoute = null;
                 self::$urlParams['action'] = array_pop($routeArr);
                 self::$urlParams['controller'] = ucfirst(array_pop($routeArr));
                 $controllerPath = '';
                 while ($dir = array_shift($routeArr)) {
                     if ($path == '/') {
                         $path .= $dir . '/';
                     } else {
                         $controllerPath .= $dir . '/';
                     }
                 }
                 self::$urlParams['controller'] = $controllerPath . self::$urlParams['controller'];
                 unset($routeArr);
             }
         } else {
             self::findAction($pathInfo, $path);
             //未匹配到路由 按文件名映射查找
         }
     } else {
         self::findAction($pathInfo, $path);
         //未匹配到路由 按文件名映射查找
     }
     $pathInfo = array_values($pathInfo);
     for ($i = 0; $i < count($pathInfo); $i += 2) {
         $_GET[$pathInfo[$i]] = $pathInfo[$i + 1];
     }
     unset($pathInfo);
     self::$urlParams['path'] = $path ? $path : '/';
     unset($path);
     $_REQUEST = array_merge($_REQUEST, $_GET);
 }