Select your language
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.
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.:
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:
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.
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() 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).
$secure = $uri->isSsl();
$internal = Uri::isInternal("myurl.org");
$currentURL = Uri::current();
and is basically equivalent to
$uri = Uri::getInstance(); $uri->toString(array('scheme','host','port','path'));
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>";
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.
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)