Back to Route class

Method link

public static string
(mixed $client, mixed $url, mixed $xhtml = true, mixed $tls = self::TLS_IGNORE, mixed $absolute = false)
Translates an internal Joomla URL to a humanly readable URL.
Parameters
  • string $client The client name for which to build the link.
  • string $url Absolute or Relative URI to Joomla resource.
  • bool $xhtml Replace & by & for XML compliance.
  • int $tls Secure state for the resolved URI. Use Route::TLS_* constants 0: (default) No change, use the protocol currently used in the request 1: Make URI secure using global secure site URI. 2: Make URI unsecure using the global unsecure site URI.
  • bool $absolute Return an absolute URL
Returns
  • string The translated humanly readable URL.
Since
  • 3.9.0
-
  • \RuntimeException
Class: Route
Project: Joomla

Method link - Source code

/**
 * Translates an internal Joomla URL to a humanly readable URL.
 * NOTE: To build link for active client instead of a specific client, you can use <var>Route::_()</var>
 *
 * @param   string   $client    The client name for which to build the link.
 * @param   string   $url       Absolute or Relative URI to Joomla resource.
 * @param   boolean  $xhtml     Replace & by & for XML compliance.
 * @param   integer  $tls       Secure state for the resolved URI. Use Route::TLS_* constants
 *                                0: (default) No change, use the protocol currently used in the request
 *                                1: Make URI secure using global secure site URI.
 *                                2: Make URI unsecure using the global unsecure site URI.
 * @param   boolean  $absolute  Return an absolute URL
 *
 * @return  string  The translated humanly readable URL.
 *
 * @throws  \RuntimeException
 *
 * @since   3.9.0
 */
public static function link($client, $url, $xhtml = true, $tls = self::TLS_IGNORE, $absolute = false)
{
    // If we cannot process this $url exit early.
    if (!\is_array($url) && strpos($url, '&') !== 0 && strpos($url, 'index.php') !== 0) {
        return $url;
    }
    // Get the router instance, only attempt when a client name is given.
    if ($client && !isset(self::$_router[$client])) {
        $app = Factory::getApplication();
        self::$_router[$client] = $app->getRouter($client);
    }
    // Make sure that we have our router
    if (!isset(self::$_router[$client])) {
        throw new \RuntimeException(Text::sprintf('JLIB_APPLICATION_ERROR_ROUTER_LOAD', $client), 500);
    }
    // Build route.
    $uri = self::$_router[$client]->build($url);
    $scheme = array('path', 'query', 'fragment');
    /*
     * Get the secure/unsecure URLs.
     *
     * If the first 5 characters of the BASE are 'https', then we are on an ssl connection over
     * https and need to set our secure URL to the current request URL, if not, and the scheme is
     * 'http', then we need to do a quick string manipulation to switch schemes.
     */
    if ($tls === self::TLS_FORCE) {
        $uri->setScheme('https');
    } elseif ($tls === self::TLS_DISABLE) {
        $uri->setScheme('http');
    }
    // Set scheme if requested or
    if ($absolute || $tls > 0) {
        static $scheme_host_port;
        if (!\is_array($scheme_host_port)) {
            $uri2 = Uri::getInstance();
            $scheme_host_port = array($uri2->getScheme(), $uri2->getHost(), $uri2->getPort());
        }
        if (is_null($uri->getScheme())) {
            $uri->setScheme($scheme_host_port[0]);
        }
        $uri->setHost($scheme_host_port[1]);
        $uri->setPort($scheme_host_port[2]);
        $scheme = array_merge($scheme, array('host', 'port', 'scheme'));
    }
    $url = $uri->toString($scheme);
    // Replace spaces.
    $url = preg_replace('/\\s/u', '%20', $url);
    if ($xhtml) {
        $url = htmlspecialchars($url, ENT_COMPAT, 'UTF-8');
    }
    return $url;
}