API15

JRoute/

From Joomla! Documentation

< API15:JRoute

The "API15" namespace is an archived namespace. This page contains information for a Joomla! version which is no longer supported. It exists only as a historical reference, it will not be improved and its content may be incomplete and/or contain broken links.

Description[edit]

Translates an internal Joomla URL to a humanly readible URL.

[<! removed edit link to red link >]

<! removed transcluded page call, red link never existed >

Syntax[edit]

_($url, $xhtml=true, $ssl=null)
Parameter Name Default Value Description
$url $url Absolute or Relative URI to resource
$xhtml true $xhtml Replace & by & for xml compilance
$ssl null $ssl Secure state for the resolved URI 1: Make URI secure using global secure site URI 0: Leave URI in the same secure state as it was passed to the function -1: Make URI unsecure using the global unsecure site URI

Returns[edit]

The translated humanly readible URL

Defined in[edit]

libraries/joomla/methods.php

Importing[edit]

jimport( 'joomla.methods' );

Source Body[edit]

function _($url, $xhtml = true, $ssl = null)
{
        // Get the router
        $app    = &JFactory::getApplication();
        $router = &$app->getRouter();

        // Make sure that we have our router
        if (! $router) {
                return null;
        }

        if ( (strpos($url, '&') !== 0 ) && (strpos($url, 'index.php') !== 0) ) {
    return $url;
        }

        // Build route
        $uri = &$router->build($url);
        $url = $uri->toString(array('path', 'query', 'fragment'));

        // Replace spaces
        $url = preg_replace('/\s/u', '%20', $url);

        /*
         * 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.
         */
        $ssl    = (int) $ssl;
        if ( $ssl )
        {
                $uri             =& JURI::getInstance();

                // Get additional parts
                static $prefix;
                if ( ! $prefix ) {
                        $prefix = $uri->toString( array('host', 'port'));
                        //$prefix .= JURI::base(true);
                }

                // Determine which scheme we want
                $scheme = ( $ssl === 1 ) ? 'https' : 'http';

                // Make sure our url path begins with a slash
                if ( ! preg_match('#^/#', $url) ) {
                        $url    = '/' . $url;
                }

                // Build the URL
                $url    = $scheme . '://' . $prefix . $url;
        }

        if($xhtml) {
                $url = str_replace( '&', '&amp;', $url );
        }

        return $url;
}

[<! removed edit link to red link >] <! removed transcluded page call, red link never existed >

Examples[edit]

Example explaining adding and removing GET parameters[edit]

//Suppose we have the current URL = .../index.php?var1=1&var2=2
JRoute::_('?var3=3')      // will replace all parameters     -> .../index.php?var3=3
JRoute::_('&var3=3')      // will add a parameter            -> .../index.php?var1=1&var2=2&var3=3
JRoute::_('&var3')        // will remove a parameter         -> .../index.php?var1=1&var2=2&var3=3
JRoute::_('&var3=3&var1') // will add and remove a parameter -> .../index.php?var2=2&var3=3

Comments[edit]

Code Examples[edit]