6

I know that you can achieve this by doing:

JFactory::getApplication->redirect(url, message, type)

but the documentation at

JFactory::getApplication

says it is deprecated and suggest to use enqueueMessage that is also deprecated and that doesn't redirect you for example to home page.

How can I redirect an user to home page with system message using methods that are not deprecated. Documentation says they will (?) be deprecated in version 4 of Joomla, so is the current method the only one or is there already a new one standard method to achieve this?

Edit: These methods aren't deprecated on JApplicationCms (Joomla libraries), but are deprecated on JApplication (Joomla Plaftorm). So since JFactory::getApplication returns JApplicationCms object, then the methods like redirect and enqueueMessage aren't deprecated and using them is the correct way to redirect with message.

5

Passing the message via the redirect function is deprecated, but both redirect() and enqueueMessage() are still both fine to use on their own.

You simply need to use both together, like so:

$app = JFactory::getApplication();

$app->enqueueMessage('Redirect to another page was successful', 'success');
$app->redirect(JRoute::_('index.php'));

enqueueMessage stores the message in a session, so when you call this before the redirect() function, the session value still exists on the page you were redirected to, thus the message will display ;)

  • 1
    Actually this is a better solution because since Joomla! 3.2 JFactory::getApplication() returns a JApplicationCms object instead of deprecated JApplication – Kitase88 Apr 7 '16 at 9:23
  • True these methods aren't deprecated on JApplicationCms (Joomla libraries), but are deprecated on JApplication (Joomla Plaftorm). Documentation link says it returns JApplicationCms (link doesn't work) and says to see JApplication where these methods are deprecated. So it's documentation confusion and I will signalize this problem, so it can be fixed. – Adam M. Apr 7 '16 at 14:17
3

The best thing to do is to handle all redirects directly in sub controllers where you just need to use $this->setRedirect($url,$msg,$type); and MVC will do the rest of the job.

If you need to redirect outside of controllers you can use JControllerLegacy class:

$controller = JControllerLegacy::getInstance("ComponentName");
$controller->setRedirect($url,$msg,$type);
$controller->redirect();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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