Issuing a redirect from a Joomla module
Asked Answered
A

1

5

I am not really familiar with Joomla but I have been tasked with writing a module which functionality is irrelevant to the question.

One of the requirements is that if the module is loaded, it should check if the user is logged in and if not - redirect him into a specific URL.

After some searching I came up with something like this, but it's obviously 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 lies in getting to the controller. Creating a new controller certainly isn't the way to go. Is there a way to get the current controller from a Joomla module and the issue a redirect?

Thank you for any answers.

Aligarh answered 8/11, 2011 at 9:53 Comment(0)
C
16

i call this static function in each of my controllers construct

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;
        }
    }
Crumple answered 8/11, 2011 at 9:57 Comment(1)
Thank you, it's exactly what I was looking for. I'll accept your answer in a couple of minutes.Aligarh

© 2022 - 2024 — McMap. All rights reserved.