简体   繁体   English

从Joomla模块发出重定向

[英]Issuing a redirect from a Joomla module

I am not really familiar with Joomla but I have been tasked with writing a module which functionality is irrelevant to the question. 我对Joomla并不熟悉,但我的任务是编写一个功能与问题无关的模块。

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. 其中一个要求是,如果模块已加载,则应检查用户是否已登录,如果没有,则将其重定向到特定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? 有没有办法从Joomla模块获取当前控制器并且问题是重定向?

Thank you for any answers. 谢谢你的任何答案。

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;
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM