JRoute/: Difference between revisions
From Joomla! Documentation
New page: ===Description===
Translates an internal Joomla URL to a humanly readible URL.
<span class="editsection" style="font-size:76%;">
<nowiki>[</nowiki>[[Description:JRoute/_|Edit Descript... |
m preparing for archive only |
||
(4 intermediate revisions by 3 users not shown) | |||
Line 2: | Line 2: | ||
Translates an internal Joomla URL to a humanly readible URL. | Translates an internal Joomla URL to a humanly readible URL. | ||
===Syntax=== | ===Syntax=== | ||
Line 23: | Line 21: | ||
| $xhtml | | $xhtml | ||
| true | | true | ||
| $xhtml Replace & by & for xml compilance. | | $xhtml Replace & by &amp; for xml compilance. | ||
|- | |- | ||
| $ssl | | $ssl | ||
Line 100: | Line 98: | ||
</source> | </source> | ||
===Examples=== | ===Examples=== | ||
=== Code Examples === | |||
<dpl> | <dpl> | ||
noresultsheader=\n | noresultsheader=\n | ||
category=_ | category=_ | ||
category=JRoute | category=JRoute | ||
namespace=CodeExample | |||
category=MethodExample | category=MethodExample | ||
include=* | include=* | ||
format= ,,, | format= ,,, | ||
</dpl> | </dpl> | ||
<source lang="php"><?php | |||
// displays the SEF URL to view article ID# 86 | |||
// 'Search Engine Friendly URLs' must be enabled in the Global config for this to have any effect | |||
echo JRoute::_('index.php?option=com_content&view=article&id=86'); | |||
?></source> |
Latest revision as of 02:01, 25 March 2017
Description
Translates an internal Joomla URL to a humanly readible URL.
Syntax
static _($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
The translated humanly readible URL.
Defined in
libraries/joomla/methods.php
Importing
jimport( 'joomla.methods' );
Source Body
public static 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.
*/
if ((int) $ssl)
{
$uri = &JURI::getInstance();
// Get additional parts.
static $prefix;
if (!$prefix)
{
$prefix = $uri->toString(array('host', 'port'));
}
// 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('&', '&', $url);
}
return $url;
}
Examples
Code Examples
<?php
// displays the SEF URL to view article ID# 86
// 'Search Engine Friendly URLs' must be enabled in the Global config for this to have any effect
echo JRoute::_('index.php?option=com_content&view=article&id=86');
?>