/** * Query Parameter Routing * * http://servername/path/?r=controller-action-id-1-name-nick * * {@inheritDoc} */ protected function match(ResultInterface $result) { $params = $result->getParameter(); if (isset($params[$this->varname])) { // set status $result->setStatus(Status::OK); // parameters $parts = explode($this->seperator, $params[$this->varname]); $controller = array_shift($parts); $action = array_shift($parts); if (count($parts) % 2) { $result->setStatus(Status::BAD_REQUEST); return false; } $newparam = []; foreach ($parts as $i => $val) { if (0 === $i % 2) { $newparam[$val] = $parts[$i + 1]; } } $result->setParameter($newparam); $result->setHandler([$controller, $action]); return true; } $result->setStatus(Status::BAD_REQUEST); return false; }
/** * {@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; }
/** * 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; }
/** * Execute dispatcher's default handler * * @return false * @access protected */ protected function defaultHandler() { if ($this->runExtensions(self::BEFORE_DEFAULT, $this->result)) { $status = $this->result->getStatus(); $handler = $this->getHandler($status); if ($handler) { $handler($this->result); } else { echo Message::get(Message::DEBUG_NEED_HANDLER, $status); } $this->runExtensions(self::AFTER_DEFAULT, $this->result); } return false; }
/** * Set collector level handler if result has no handler set yet * * @param ResultInterface $result desc * @return self * @access protected */ protected function setCollectorHandler(ResultInterface $result) { $status = $result->getStatus(); if (is_null($result->getHandler()) && $this->getHandler($status)) { // debug message $this->debug(Message::get(Message::DEBUG_SET_C_HANDLER, get_class($this), $status)); $result->setHandler($this->getHandler($status)); } return $this; }
/** * 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; }
/** * {@inheritDoc} */ public function __invoke(ResultInterface $result, $stage = '') { echo sprintf("%s(%d) ", $stage, $result->getStatus()); return true; }