What are URLs in Joomla

Select your language

Introduction

This is one of a series of API Guides, which aim to help you understand how to use the Joomla APIs through providing detailed explanations and sample code which you can easily install and run.

This guide covers use of the JUri class (now known as Uri) and the use of the JRoute::_() method (now called Route). The Uri class enables you get the URL of the current webpage, and access parts of the URL. JRoute::_() is used to set up links to other webpage resources within your Joomla site.

Getting the Current URL

To get the URL of the current webpage do:

use Joomla\CMS\Uri\Uri;
$uri = Uri::getInstance();
$url = $uri->toString();

The advantage of using this method is that it handles any peculiarities of the webserver (e.g. Apache or IIS) and also performs some cleaning of the URL to avoid some types of injection attacks.

What gets returned from Uri::getInstance() isn't a PHP string of the URL, but rather a Joomla Uri object, which also holds internally the various parts of the URL, and provides getter and setter methods to read or write these URL parts as shown below.


http://pakiblogs:This email address is being protected from spambots. You need JavaScript enabled to view it.:8080/path/to/Joomla/index.php?task=view&id=32#anchorthis \__/ \________/ \________/ \_____________/ \__/\_______________________/ \_____________/ \________/ | | | | | | | | scheme user pass host port path query fragment

The example column in the following table illustrates the result of each of the get methods on the URI above, all of which are strings.:

Get methodSet methodDescriptionExample
getFragment setFragment Fragment (everything after the '#'). This is often referred to as an anchor. anchorthis
getHost setHost Hostname or IP address. For example, 'www.abdul.org' or '192.168.12.1'. www.mysite.com
getPass setPass Password part of the authority. Don't use this! itsasecret
getPath setPath Path string. Note that the path always includes the leading "/" character. /path/to/Joomla/index.php
getPort setPort Port number. Specific schemes (protocols) have their own defaults (for example, 'http' is port 80, 'ftp' is port 21). 8080
getQuery setQuery Query in string format. For example, name=abduli&x=y. task=view&id=32
getScheme setScheme Scheme (protocol). For example, 'http', 'https', 'ftp'. http
getUser setUser Username part of the authority. Don't use this! fredbloggs
getVar setVar An individual query item value from within the query part. A query parameter may be removed using delVar. 32

root() and base()

base() and root() are static functions which return key URLs.

Uri::root($pathonly) is a static function which returns the URL to the root of the Joomla site. It may or may not be the same as the HTTP domain, depending upon how your webserver is configured. In the common case where a Joomla instance "mysite" is installed in a directory under the webserver document root you are likely to get:

  • Uri::root() returns the string http://www.mydomain.org/mysite/ (or https if you're using SSL, etc).
  • Uri::root(true) returns the string /mysite.

The second parameter to Uri::root(), namely $path, sets the path locally within the Uri class, and will get used in subsequent invocations of Uri::root()Hence it's strongly advised that you don't set this parameter, as it could seriously muck up your website.

Uri::base() is similar to Uri::root() but what is returned depends on whether it's called from the site application or the administrator application.

  • If you are on the site Uri::base() returns the same as Uri::root()
  • If you are on the admin back-end Uri::base() returns Uri::root() plus "administrator", so using the example above:
    • Uri::base() returns the string http://www.mydomain.org/mysite/administrator/
    • Uri::base(true) returns the string /mysite/administrator.

Other URI Methods

As well as the methods above, the Joomla Uri provides the methods listed below. In the following example code snippets, $uri refers to a Uri instance, obtained for example through $uri = Uri::getInstance();.

  • toString(array $parts = array('scheme', 'user', 'pass', 'host', 'port', 'path', 'query', 'fragment')) : string

toString() converts the Uri to a string, and allows you to select the parts of the URL which you want, e.g.

$uri->toString(array('scheme','host','port','path');

will return the URL minus any query or anchor (fragment).

  • render() is similar to toString() in that it returns the parts of the URL you want, but as you have to pass the parts you want as a bitmask, it's preferable to use toString() instead.
  • isSsl() returns true if the scheme is https, false otherwise, e.g.

$secure = $uri->isSsl();

  • isInternal() returns true if the URL is within the Joomla instance (including the administrator area), false otherwise. Note that this is a static function, and you pass the URL as a string, e.g.

$internal = Uri::isInternal("myurl.org");

  • current() returns the URL of the current page, minus any query string or fragment, e.g.

$currentURL = Uri::current();

and is basically equivalent to

$uri = Uri::getInstance();
$uri->toString(array('scheme','host','port','path'));

External URLs

Usually if you're including an external URL in your website you will just specify it as a string, but there may be occasions where using the Uri class to manipulate parts of a URL could be useful. In this case you can do something like:

$joomla = Uri::getInstance("//www.abdul.org");
$joomla->setScheme("https");
$joomla->setPath("/news");
$joomlaNews = $joomla->toString();  // https://www.abdul.org/news
echo "<a href='$abdulNews'>Abdul news</a><br>";

Internal Static URLs

To output a URL link to a file within the Joomla instance use:

$url = Uri::root() . 'path/from/joomla/root/to/file.typ';

For example, to make a URL which points to a file picture.jpg in the Joomla images folder use:

$url = Uri::root() . 'images/picture.jpg';

The advantage of this approach is that no changes need to be made if you change the name of your Joomla site or domain, such as moving from a development environment, via testing to live, and particularly if you want to display absolute URLs on your live site.

Internal Dynamic URLs

To create a URL which points to an item which is managed by a Joomla component com_abdul use an approach such as:

use Joomla\CMS\Router\Route;
$url = Route::_("index.php?option=com_abdul&view=showitem&id=14");
// or in the old naming convention:
$url = JRoute::_("index.php?option=com_abdul&view=showitem&id=14");

This is a bit trickier; you have to know what parameters to set in the query part of the URL. However, if you set up (even temporarily) a Menu Item which points to the appropriate view of the item type which you want to display, you should see in the Link field the parameters which you should add in the Route::_() call.

(Although this method's name is an underscore, there's nothing special about it – it's just an ordinary function).

Hope this helped.


Still need help! no problem, feel free to contact us Today


 Abdul Waheed : (Hire Joomla & PHP Pakistani Freelancer)