/**
  * {@inheritDoc}
  */
 public function __invoke(ResultInterface $result, $stage = '')
 {
     if (!$result->getRequest()->getServerInfo('https') && $stage) {
         $result->setStatus(Status::MOVED_PERMANENTLY)->setParameter('__REDIRECT_URL__', $this->url ?: $this->getUrl((array) $result->getRequest()->getServerInfo()))->setHandler(function (ResultInterface $result) {
             header("Location: " . $result->getParameter('__REDIRECT_URL__'), true, Status::MOVED_PERMANENTLY);
             die;
         });
         // skip rest of the match/dispatch procedure
         return false;
     }
     return true;
 }
Пример #2
0
 /**
  * Parameter Pairs Routing (PPR)
  *
  * /path/index.php/controller/action/id/1/name/nick
  *
  * {@inheritDoc}
  */
 protected function match(ResultInterface $result)
 {
     $path = trim($result->getRequest()->getServerInfo('path_info'), ' /');
     if (count($parts = explode('/', $path)) > 1) {
         // set status
         $result->setStatus(Status::OK);
         // parameters
         $controller = array_shift($parts);
         $action = array_shift($parts);
         if (count($parts) % 2) {
             $result->setStatus(Status::BAD_REQUEST);
             return false;
         }
         $params = [];
         foreach ($parts as $i => $val) {
             if (0 === $i % 2) {
                 $params[$val] = $parts[$i + 1];
             }
         }
         $result->setParameter($params);
         $result->setHandler([$controller, $action]);
         return true;
     }
     $result->setStatus(Status::BAD_REQUEST);
     return false;
 }
Пример #3
0
 /**
  * Get matched route
  *
  * @param  ResultInterface $result
  * @param  string $routeKey unique route key
  * @param  array $matches matched parameters
  * @return bool
  * @access protected
  */
 protected function getRoute(ResultInterface $result, $routeKey, array $matches)
 {
     $request = $result->getRequest();
     $method = $request->getHttpMethod();
     // matched but method not allowed
     if (!isset($this->routes[$routeKey][$method])) {
         $result->setStatus(Status::METHOD_NOT_ALLOWED);
         return false;
     }
     // get the route
     $route = $this->routes[$routeKey][$method];
     // apply others filters
     if (!$this->filtering($route, $request)) {
         $status = Status::PRECONDITION_FAILED;
         $result->setStatus($status)->setHandler($route->getHandler($status));
         return false;
     }
     // remember the route
     $result->setRoute($route);
     // set result's parameters
     $result->setParameter(array_replace($route->getDefault(), $matches));
     // set status & handler
     $status = Status::OK;
     $result->setStatus($status)->setHandler($route->getHandler(Status::OK));
     return true;
 }