25

The following code works in Joomla 1.6, but it throws up a Strict Standards error in 3.x:

Strict Standards: Only variables should be assigned by reference

    //Get the full URL for sharing purposes
    $uri = & JFactory::getURI();
    $absolute_url = $uri->toString();

    <?php echo $absolute_url; ?>

How can I get the code to work without the error in Joomla 3.x?

3
  • Btw. it doesn't depend on Joomla version but by PHP version. Assigning variables by reference is deprecated in PHP
    – turson
    Sep 2, 2014 at 14:02
  • 3
    Just a note to anyone who seeks an answer for this, to get the FULL URL, do not use JUri::current() as it won't return any query parameters. Simply use what is described in the accepted answer below: joomla.stackexchange.com/a/4295/168
    – Lodder
    Oct 10, 2016 at 12:01
  • Best to see api doc api.joomla.org/cms-3/classes/Joomla.CMS.Uri.Uri.html
    – Anant
    Jan 10, 2018 at 14:20

4 Answers 4

43

UPDATE

The answer to the main question is: JFactory::getUri() is deprecated, use JUri directly like this:

<?php 
$uri = JUri::getInstance(); 
echo $uri->toString();

// or simpler:

echo JUri::getInstance();
?>

The Strict Standards error is because of the & before JFactory::getURI();. You can find more information about that below.


ORIGINAL ANSWER

Delete the &

//Get the full URL for sharing purposes
$uri = JFactory::getURI();
$absolute_url = $uri->toString();

<?php echo $absolute_url; ?>

With PHP5 the new operator automatically returns a reference, so the '&' is not needed anymore.

From the official PHP manual:

As of PHP 5, the new operator returns a reference automatically, so assigning the result of new by reference results in an E_DEPRECATED message in PHP 5.3 and later, and an E_STRICT message in earlier versions.

source: http://php.net/manual/en/language.operators.assignment.php

1
7

According to the Joomla tutorial, you can get the current URL by this function:

JUri::current();
1
5

UPDATE: JUri is now also deprecated. The correct way now is as follows:

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

(Edited to include an initial backslash - required if there is a current namespace)

Old message that is now also deprecated:

JFactory::getURI(); is deprecated. The correct method to get the current URL is:

$uri = JUri::getInstance();
echo  $uri->toString();
0

Relying on the toString method to get the current URL is wrong. The toString method is not meant for that. The following is a better, cleaner, and future proof approach to retrieve the current URL:

$objJURI = JFactory::getURI();
$strQuery = '';
if (!empty($objJURI->getQuery()))
    $strQuery = '?'.$objJURI->getQuery();
$strFragment = '';
if ((!empty($objJURI->getFragment())) && ($objJURI->getFragment() != '#'))
    $strFragment = $objJURI->getFragment();
$currentURL = $objJURI->current().$strQuery.$strFragment;

Note that the getFragment method should return the part after the number sign, but there seems to be a core bug that prevents it from doing that.

2
  • This is wrong. It will display index.php&option=com_foo&view=bar. It should be ? after index.php
    – Lodder
    Oct 10, 2016 at 11:55
  • 2
    Its absolutely legit to use toString. Its a public method and heavily used by the JUri class itself. Even the method comment says "Returns full uri string.". Using JFactory::getUri(); is deprecated and definitely "more wrong".
    – fruppel
    Oct 10, 2016 at 17:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.