Aerys\Router::route PHP Method

route() public method

The variadic ...$actions argument allows applications to specify multiple separate handlers for a given route URI. When matched these action callables will be invoked in order until one starts a response. If the resulting action fails to send a response the end result is a 404. Matched URI route arguments are made available to action callables as an array in the following Request property: $request->locals->routeArgs array. Route URIs ending in "/?" (without the quotes) allow a URI match with or without the trailing slash. Temporary redirects are used to redirect to the canonical URI (with a trailing slash) to avoid search engine duplicate content penalties.
public route ( string $method, string $uri, $actions ) : Router
$method string The HTTP method verb for which this route applies
$uri string The string URI
$actions The action(s) to invoke upon matching this route
return Router
    public function route(string $method, string $uri, ...$actions) : Router
    {
        if ($this->state !== Server::STOPPED) {
            throw new \LogicException("Cannot add routes once the server has started");
        }
        if ($method === "") {
            throw new \DomainException(__METHOD__ . " requires a non-empty string HTTP method at Argument 1");
        }
        if (empty($actions)) {
            throw new \DomainException(__METHOD__ . " requires at least one callable route action or middleware at Argument 3");
        }
        $actions = array_merge($this->actions, $actions);
        $uri = "/" . ltrim($uri, "/");
        // Special-case, otherwise we redirect just to the same URI again
        if ($uri === "/?") {
            $uri = "/";
        }
        if (substr($uri, -2) === "/?") {
            $canonicalUri = substr($uri, 0, -2);
            $redirectUri = substr($uri, 0, -1);
            $this->routes[] = [$method, $canonicalUri, $actions];
            $this->routes[] = [$method, $redirectUri, [static function (Request $request, Response $response) {
                $uri = $request->getUri();
                if (stripos($uri, "?")) {
                    list($path, $query) = explode("?", $uri, 2);
                    $path = rtrim($path, "/");
                    $redirectTo = "{$path}?{$query}";
                } else {
                    $redirectTo = $path = substr($uri, 0, -1);
                }
                $response->setStatus(HTTP_STATUS["FOUND"]);
                $response->setHeader("Location", $redirectTo);
                $response->setHeader("Content-Type", "text/plain; charset=utf-8");
                $response->end("Canonical resource URI: {$path}");
            }]];
        } else {
            $this->routes[] = [$method, $uri, $actions];
        }
        return $this;
    }