Autarky\Routing\UrlGenerator::getRouteUrl PHP Method

getRouteUrl() public method

Get the URL to a named route.
public getRouteUrl ( string $name, array $params = [], boolean $relative = false ) : string
$name string
$params array
$relative boolean
return string
    public function getRouteUrl($name, array $params = array(), $relative = false)
    {
        $route = $this->router->getRoute($name);
        $routeParams = [];
        $query = [];
        foreach ($params as $key => $value) {
            if (is_int($key)) {
                $routeParams[] = $value;
            } else {
                $query[$key] = $value;
            }
        }
        $path = $this->routePathGenerator->getRoutePath($route, $routeParams);
        if ($query) {
            $path .= '?' . http_build_query($query);
        }
        if ($relative) {
            $root = $this->requests->getCurrentRequest()->getBaseUrl();
        } else {
            $root = $this->getRootUrl();
        }
        return $root . $path;
    }

Usage Example

 /**
  * Implementation of the `url(route)` twig function.
  *
  * @param  string  $name       Route name
  * @param  array   $parameters Route parameters
  * @param  boolean $relative   Whether to generate an absolute or relative URL.
  *
  * @return string
  */
 public function getUrl($name, $parameters = array(), $relative = false)
 {
     return $this->urlGenerator->getRouteUrl($name, $parameters, $relative);
 }