URLs in Joomla

From Joomla! Documentation

Revision as of 11:12, 18 December 2019 by Robbiej (talk | contribs) (Page creation with first 2 sections)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Introduction[edit]

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[edit]

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 (eg Apache or IIS) and also performs some cleaning of the URL to avoid some types of injection attacks.

What get returns 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://fredbloggs:itsasecret@www.example.com: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 method Set method Description Example
getFragment setFragment Fragment (everything after the '#'). This is often referred to as an anchor. anchorthis
getHost setHost Hostname or IP address. For example, 'www.joomla.org' or '192.168.2.45'. www.example.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, "foo=bar&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

(Note that passing the user/password in the URL in this fashion is generally deprecated and not recommended, and won't work on some browsers. In this guide below it's not considered.)