Пример #1
0
 /**
  * Parses a URL based on this rule.
  * @param CUrlManager  $manager     the URL manager
  * @param CHttpRequest $request     the request object
  * @param string       $pathInfo    path info part of the URL (URL suffix is already removed based on {@link CUrlManager::urlSuffix})
  * @param string       $rawPathInfo path info that contains the potential URL suffix
  * @return mixed the route that consists of the controller ID and action ID. False if this rule does not apply.
  */
 public function parseUrl($manager, $request, $pathInfo, $rawPathInfo)
 {
     $paths = explode('/', $pathInfo);
     if ($paths[0] != "api") {
         return false;
     }
     $controller = $paths[1];
     if (array_search($controller, $this->restControllers) === false) {
         return false;
     }
     switch ($request->getRequestType()) {
         case 'GET':
             if (count($paths) == 2) {
                 return $controller . "/restList";
             } else {
                 if (count($paths) > 2) {
                     $_GET['id'] = $paths[2];
                     if (isset($paths[3])) {
                         $_GET['var'] = $paths[3];
                     }
                     if (isset($paths[4])) {
                         $_GET['var2'] = $paths[4];
                     }
                     return $controller . "/restView";
                 }
             }
             break;
         case 'PUT':
             if (count($paths) >= 3) {
                 $_GET['id'] = $paths[2];
                 if (isset($paths[3])) {
                     $_GET['var'] = $paths[3];
                 }
                 return $controller . "/restUpdate";
             }
             break;
         case 'POST':
             if (count($paths) >= 2) {
                 if (isset($paths[2])) {
                     $_GET['id'] = $paths[2];
                 }
                 return $controller . "/restCreate";
             }
             break;
         case 'DELETE':
             if (count($paths) == 2) {
                 $_GET['id'] = $paths[2];
                 return $controller . "/restDelete";
             }
             break;
     }
     return false;
     // this rule does not apply
 }
Пример #2
0
 /**
  * Parses a URL based on this rule.
  * @param CUrlManager $manager the URL manager
  * @param CHttpRequest $request the request object
  * @param string $pathInfo path info part of the URL
  * @param string $rawPathInfo path info that contains the potential URL suffix
  * @return mixed the route that consists of the controller ID and action ID or false on error
  */
 public function parseUrl($manager, $request, $pathInfo, $rawPathInfo)
 {
     if ($this->verb !== null && !in_array($request->getRequestType(), $this->verb, true)) {
         return false;
     }
     if ($manager->caseSensitive && $this->caseSensitive === null || $this->caseSensitive) {
         $case = '';
     } else {
         $case = 'i';
     }
     if ($this->urlSuffix !== null) {
         $pathInfo = $manager->removeUrlSuffix($rawPathInfo, $this->urlSuffix);
     }
     // URL suffix required, but not found in the requested URL
     if ($manager->useStrictParsing && $pathInfo === $rawPathInfo) {
         $urlSuffix = $this->urlSuffix === null ? $manager->urlSuffix : $this->urlSuffix;
         if ($urlSuffix != '' && $urlSuffix !== '/') {
             return false;
         }
     }
     if ($this->hasHostInfo) {
         $pathInfo = strtolower($request->getHostInfo()) . rtrim('/' . $pathInfo, '/');
     }
     $pathInfo .= '/';
     if (preg_match($this->pattern . $case, $pathInfo, $matches)) {
         foreach ($this->defaultParams as $name => $value) {
             if (!isset($_GET[$name])) {
                 $_REQUEST[$name] = $_GET[$name] = $value;
             }
         }
         $tr = array();
         foreach ($matches as $key => $value) {
             if (isset($this->references[$key])) {
                 $tr[$this->references[$key]] = $value;
             } elseif (isset($this->params[$key])) {
                 $_REQUEST[$key] = $_GET[$key] = $value;
             }
         }
         if ($pathInfo !== $matches[0]) {
             // there're additional GET params
             $manager->parsePathInfo(ltrim(substr($pathInfo, strlen($matches[0])), '/'));
         }
         if ($this->routePattern !== null) {
             return strtr($this->route, $tr);
         } else {
             return $this->route;
         }
     } else {
         return false;
     }
 }
Пример #3
0
 /**
  * @inheritDoc
  */
 public function getRequestType()
 {
     if ($this->_requestType === null) {
         return parent::getRequestType();
     } else {
         return $this->_requestType;
     }
 }