This is in my Controller. Currently, I am redirecting using the inherited function setRedirect(URL_TO_REDIRECT_TO).

public function redirectTo($layout){
    $this->setRedirect('index.php?option=com_component&view=view_name&layout=' . $layout);

In what way can I IMPROVE this (if it is the correct way of redirecting)?

I am learning Joomla MVC and would like to improve my skills.

Thank you. That really clarified a lot for me. The reason I am using this method of doing it, is mainly to also pass parameters through the URL if needed.

So my next part of the function looks like this

public function redirectTo($layout, $id = null){
    $url = 'index.php?option=com_component&view=view_name&layout=' . 
        $url =  $url . "&id=" .  $id;

Is this Okay? Or is there already a method made for this? I think I am going into a URL builder with this.

1 Answer 1


You should run the URL through Joomla\CMS\Router\Route::_() before passing to setRedirect() to get the correct SEF URL since none of the involved methods do that automatically.

use Joomla\CMS\Router\Route;

$this->setRedirect(Route::_('index.php?option=com_component&view=view_name&layout=' . $layout));

The rest is correct, if not somewhat unnecessary. setRedirect() is used to set a redirect which is performed after the controller's task has been completed. And it accepts additional parameters for setting a message and its type. Your method does not have these parameters and it performs the redirect instantly. So realistically you could remove your custom method and just call application's redirect() method to get the same result.

$this->app->redirect(Route::_('index.php?option=com_component&view=view_name&layout=' . $layout));
  • I have made some changes to the top question. If you have time, Can you give me clarity on it?
    – DeesreX
    Commented Aug 12, 2022 at 10:24

Your Answer

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

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