src/Smrtr/HaltoRouter.php
public function generate($routeName, array $params = array(), $hostname = null, $protocol = '//', $port = null)
    {
        // Check if named route exists
        if(!isset($this->namedRoutes[$routeName])) {
            throw new HaltoRouterException("Route '{$routeName}' does not exist.");
        }

        // Replace named parameters
        $hostGroup = $this->namedRoutes[$routeName][1];
        $route = $this->namedRoutes[$routeName][0];
        $url = $route;

        if (preg_match_all('`(/|\.|)\[([^:\]]*+)(?::([^:\]]*+))?\](\?|)`', $route, $matches, PREG_SET_ORDER)) {

            foreach($matches as $match) {
                list($block, $pre, $type, $param, $optional) = $match;

                if ($pre) {
                    $block = substr($block, 1);
                }

                if(isset($params[$param])) {
                    $url = str_replace($block, $params[$param], $url);
                } elseif ($optional) {
                    $url = str_replace($pre . $block, '', $url);
                }
            }


        }

        // Try to include the hostname and protocol
        $hasHostPart = false;
        if (is_string($hostGroup) && array_key_exists($hostGroup, $this->httpHostGroups)) {

            $hasHostPart = true;

            if (is_string($hostname) && in_array($hostname, $this->httpHostGroups[$hostGroup])) {
                $hostPart = rtrim($hostname, '/');
            } else {
                $hostPart = $this->httpHostGroups[$hostGroup][0];
            }

            if (is_int($port)) {
                $hostPart .= ':' . $port;
            }

            $url = $hostPart . '/' . ltrim($url, '/');
        }

        if ($hasHostPart) {
            $url = $protocol . $url;
        }

        return $url;
    }
public function generate($routeName, array $params = array(), $hostname = null, $protocol = '//', $port = null)
    {
        // Check if named route exists
        if(!isset($this->namedRoutes[$routeName])) {
            throw new HaltoRouterException("Route '{$routeName}' does not exist.");
        }

        // Replace named parameters
        $hostGroup = $this->namedRoutes[$routeName][1];
        $route = $this->namedRoutes[$routeName][0];
        $url = $route;

        if (preg_match_all('`(/|\.|)\[([^:\]]*+)(?::([^:\]]*+))?\](\?|)`', $route, $matches, PREG_SET_ORDER)) {

            foreach($matches as $match) {
                list($block, $pre, $type, $param, $optional) = $match;

                if ($pre) {
                    $block = substr($block, 1);
                }

                if(isset($params[$param])) {
                    $url = str_replace($block, $params[$param], $url);
                } elseif ($optional) {
                    $url = str_replace($pre . $block, '', $url);
                }
            }


        }

        // Try to include the hostname and protocol
        $hasHostPart = false;
        if (is_string($hostGroup) && array_key_exists($hostGroup, $this->httpHostGroups)) {

            $hasHostPart = true;

            if (is_string($hostname) && in_array($hostname, $this->httpHostGroups[$hostGroup])) {
                $hostPart = rtrim($hostname, '/');
            } else {
                $hostPart = $this->httpHostGroups[$hostGroup][0];
            }

            if (is_int($port)) {
                $hostPart .= ':' . $port;
            }

            $url = $hostPart . '/' . ltrim($url, '/');
        }

        if ($hasHostPart) {
            $url = $protocol . $url;
        }

        return $url;
    }
Reversed routing

Generate the URL for a named route. Replace regexes with supplied parameters.

@param string $routeName The name of the route.
@param array  $params Associative array of parameters to replace placeholders with.
@param string $hostname Optional; specify a valid hostname or one will be detected automatically if possible.
@param string $protocol Specify a protocol including "://" if you wish
@param int    $port Optional; specify a port to use in the generated URL

@return string The URL of the route with named parameters in place.

@throws HaltoRouterException