1

I'm trying to understand the tutorial at https://docs.joomla.org/J3.x:Developing_an_MVC_Component/Adding_a_view_to_the_site_part, but I don't understand the last line of code ($controller->redirect();).

<?php
/**
 * @package     Joomla.Administrator
 * @subpackage  com_helloworld
 *
 * @copyright   Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 */

// No direct access to this file
defined('_JEXEC') or die('Restricted access');

// Get an instance of the controller prefixed by HelloWorld
$controller = JControllerLegacy::getInstance('HelloWorld');

// Perform the Request task
$input = JFactory::getApplication()->input;
$controller->execute($input->getCmd('task'));

// Redirect if set by the controller
$controller->redirect();

I looked at https://api.joomla.org/cms-3/classes/Joomla.CMS.MVC.Controller.BaseController.html#method_redirect to find what this redirect() actually does, but it does nothing and just returns a boolean value or maybe I am misunderstanding.

I wonder where this redirect() leads to. Does this redirect() follow certain rules that I didn't yet see? Does redirect() lead to a fixed URL?.

1

It redirects to whatever is set in controller's $redirect property, normally using setRedirect() method.

As an example, redirects are commonly used in form controllers. When saving an item fails, you are usually redirected back to the form. And when saving succeeds, you are redirected to a list of items.

2

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.