Issuing redirects from Joomla module

I am not very familiar with Joomla, but I was instructed to write a module whose functionality is not relevant to the issue.

One of the requirements is that if the module is loaded, it must check whether the user is registered, and if not, redirect it to a specific URL.

After some searches, I came up with something like this, but this is clearly not a working answer:

$user =& JFactory::getUser();

if (!$user->id) {
    include_once JPATH_COMPONENT . DIRECTORY_SEPARATOR . "controller.php"; // assuming com_content
    $contentController = new ContentController();
    $link = JRoute::_("my url");
    $contentController->setRedirect($link);
    return;
}

I think the problem is getting to the controller. Of course, creating a new controller is not an option. Is there a way to get the current controller from the Joomla module and a redirect problem?

Thanks for any answers.

+5
source share
1 answer

static function forceLoggedIn(){


    $user = JFactory::getUser();

        if($user->guest||$user->id == 0)
        {
            $error = JText::_('YOU MUST BE LOGGED IN');
            //base xkè altrimenti andrebbe in loop di redirect
            JFactory::getApplication()->redirect(JURI::base(), $error, 'error' );
            return false;
        }
    }
+16

All Articles