J4 redirect Topic is solved

For Joomla! 4.x Coding related discussions, you could also use: http://groups.google.com/group/joomla-dev-general

Moderator: ooffick

Forum rules
Post Reply
yann112
Joomla! Apprentice
Joomla! Apprentice
Posts: 19
Joined: Mon Aug 08, 2022 8:18 am

J4 redirect

Post by yann112 » Mon Sep 26, 2022 3:41 pm

Hello,

I try how to make a redirect in J4.
Until now, I used this code in J3 :

Code: Select all

$app = JFactory::getApplication();
$app->redirect('index.php?option=com_component&my_view', 'message text', 'error');
$app->close();
How do a redirect (with url + message + type message) in J4 ?

Thank in advance for your help

User avatar
Per Yngve Berg
Joomla! Master
Joomla! Master
Posts: 29475
Joined: Mon Oct 27, 2008 9:27 pm
Location: Romerike, Norway

Re: J4 redirect

Post by Per Yngve Berg » Mon Sep 26, 2022 4:49 pm

You are using the API version from before J3.8.

https://api.joomla.org/cms-4/classes/Jo ... ctory.html

yann112
Joomla! Apprentice
Joomla! Apprentice
Posts: 19
Joined: Mon Aug 08, 2022 8:18 am

Re: J4 redirect

Post by yann112 » Tue Sep 27, 2022 5:10 am

@Per Yngve Berg
Thank you for your answer.
Yes it's the code of component J3.
My question is how to use it with the latest version of the API.



In J3, the $app->redirect(url+msg+msg type) works everywhere. (view/hepler/controller)
In J4 the $app->redirect(url+msg+msg type) not work in controller, error below :

"An error has occurred. 0 You have not supplied a valid HTTP status code"

Based on another answer :
"The same way as in Joomla 3 https://github.com/joomla/joomla-cms/bl ... .php#L1043"
So in J4 for the redirect works in the controller with $this->setRedirect(url+msg+msg type)

Another different behaviour constated, in J3 the $app->redirect(...) support (url+msg+msg type), not in J4 (url+code redirect).

So based on this, the rules for J4 are :

-> For view/model/helper : $app->redirect(url + code redirect) and set the message system by another way.
-> For the controller : $this->setRedirect(url+msg+msg type)

If anyone have another idea, don't hesitate to share it.

Thank in advance

User avatar
Per Yngve Berg
Joomla! Master
Joomla! Master
Posts: 29475
Joined: Mon Oct 27, 2008 9:27 pm
Location: Romerike, Norway

Re: J4 redirect

Post by Per Yngve Berg » Tue Sep 27, 2022 5:28 am

JFactory was removed in 4.0.

You must use the namespaced classes.

SharkyKZ
Joomla! Hero
Joomla! Hero
Posts: 2441
Joined: Fri Jul 05, 2013 10:35 am
Location: Parts Unknown

Re: J4 redirect

Post by SharkyKZ » Tue Sep 27, 2022 5:40 am

yann112 wrote:
Tue Sep 27, 2022 5:10 am
@Per Yngve Berg
Thank you for your answer.
Yes it's the code of component J3.
My question is how to use it with the latest version of the API.



In J3, the $app->redirect(url+msg+msg type) works everywhere. (view/hepler/controller)
In J4 the $app->redirect(url+msg+msg type) not work in controller, error below :

"An error has occurred. 0 You have not supplied a valid HTTP status code"

Based on another answer :
"The same way as in Joomla 3 https://github.com/joomla/joomla-cms/bl ... .php#L1043"
So in J4 for the redirect works in the controller with $this->setRedirect(url+msg+msg type)

Another different behaviour constated, in J3 the $app->redirect(...) support (url+msg+msg type), not in J4 (url+code redirect).

So based on this, the rules for J4 are :

-> For view/model/helper : $app->redirect(url + code redirect) and set the message system by another way.
-> For the controller : $this->setRedirect(url+msg+msg type)

If anyone have another idea, don't hesitate to share it.

Thank in advance
Use $app->enqueueMessage($msg, $msgType) to set a message.

https://api.joomla.org/cms-4/classes/Jo ... eueMessage
https://api.joomla.org/framework-2/clas ... d_redirect

For what it's worth, you should avoid setting messages and performing redirects outside of the controller.

yann112
Joomla! Apprentice
Joomla! Apprentice
Posts: 19
Joined: Mon Aug 08, 2022 8:18 am

Re: J4 redirect

Post by yann112 » Tue Sep 27, 2022 6:01 am

@Per Yngve Berg
Thank you, noted

@SharkyKZ
Thank you for the advice and the function


Post Reply

Return to “Joomla! 4.x Coding”