Joomla PHP插件如何检查当前组件是不是com_users

问题描述 投票:0回答:1

所以我有以下代码导致无限重定向循环,因为我无法检查用户所在的当前URL是否是“com_users”组件。

如果它们在com_users组件上,我不希望再执行任何代码。

public function onAfterInitialise() {
    $app  = JFactory::getApplication();
    $user = JFactory::getUser();
    if (!$user->guest) {
        //THIS CAN'T GET CURRENT COMPONENT AND CAUSES INFINITE redirect LOOP
        if ( !($app->input->get('option') == 'com_users' && JRequest::getVar('view') == 'profile') ) { //IF NOT on the EDIT PROFILE URL then force the user to go and change their email
            if ($user->email === "[email protected]") {
                $app->enqueueMessage('Please change your email address!');
                $app->redirect(
                    JRoute::_(
                        'index.php?option=com_users&view=profile&layout=edit'
                    )
                );
            }
        }
    }
}
php joomla
1个回答
1
投票

使用观察者来降低复杂性。

JRequest已弃用,请改用$app->inputInput::getCmd()做了一些自动卫生。

public function onAfterInitialise()
{
    $user = JFactory::getUser();
    $app  = JFactory::getApplication();

    if ($user->guest)
    {
        return;
    }

    if ($app->input->getCmd('option') === 'com_users' && $app->input->getCmd('view') === 'profile')
    {
        return;
    }

    if ($user->email === "[email protected]")
    {
        $app->enqueueMessage('Please change your email address!');
        $app->redirect(
            JRoute::_(
                'index.php?option=com_users&view=profile&layout=edit'
            )
        );
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.