2

I have a number of menus that point to the users registration component. Depending on where the user registers I redirect them accordingly with a custom plugin.

public function onUserAfterSave($user, $isnew, $success, $msg)
{

    //get the active menu item id so we can compare what plan we are on
    $menu   = $app->getMenu();
    $active   = $menu->getActive();
    $activeId = $active->id;

    //compare active page (i.e. what menu item are we on) with the parameter set in the plugin
    //if it matches redirect accordingly - fallback is to always redirect to free plan
    if ($activeId == $this->params->get('free-reg-menu'))
    {
        //redirect required to go to main page
        JFactory::getApplication()->redirect(JRoute::_('page1'));
    }
    else if ($activeId == $this->params->get('bv-reg-menu')) 
    {
        //redirect to step 2 of main plan payment processing
        JFactory::getApplication()->redirect(JRoute::_('page2'));
    }
    else if ($activeId == $this->params->get('prem-reg-menu'))
    {
        //redirect to step 2 of value plan payment processing
        JFactory::getApplication()->redirect(JRoute::_('page3'));
    }
    else
    {
        //other stuff
    }

}

Depending on what menus were assigned in this plugin I redirect the user to a specific page. This all works quite well.

My problem is when registration fails (ie. duplicate username, wrong matching passwords etc.) the page does a refresh due to the server side validation. This refresh brings the page away from the original link. It seem to redirect to the first registration menu page I created.

For example: I'm on a registration page and I enter wrong credentials then the page refreshes to a completely different page. Any ideas? I guess an extension for AJAX registration might address this but I'd like to avoid that for now.

4

2 回答 2

1

您可以执行以下操作:

  • 改为在onAfterRoute中执行检查
  • 将结果保存在用户会话中
  • 结果成功时从onUserAfterSave进行重定向

我想不出更简单的方法或它带来的任何复杂性。


系统插件

<?php
public function onAfterRoute()
{
    $app = JFactory::getApplication();

    if ($app->isSite() == false)
        return;

    //get the active menu item id so we can compare what plan we are on
    $menu   = $app->getMenu();
    $active   = $menu->getActive();
    $activeId = $active->id;

    $finalRedirect = null;

    //compare active page (i.e. what menu item are we on) with the parameter set in the plugin
    //if it matches redirect accordingly - fallback is to always redirect to free plan
    if ($activeId == $this->params->get('free-reg-menu'))
    {
        //redirect required to go to main page
        $finalRedirect = JRoute::_('page1');
    }
    else if ($activeId == $this->params->get('bv-reg-menu')) 
    {
        //redirect to step 2 of main plan payment processing
        $finalRedirect = JRoute::_('page2');
    }
    else if ($activeId == $this->params->get('prem-reg-menu'))
    {
        //redirect to step 2 of value plan payment processing
        $finalRedirect = JRoute::_('page3');
    }
    else
    {
        //other stuff
    }

    if($finalRedirect)
    {
        // Store selection in session
        JFactory::getSession()->set('my.redirection', $finalRedirect);
    }
}

用户插件

<?php
public function onUserAfterSave($user, $isnew, $success, $msg)
{
    $session = JFactory::getSession();

    if ($finalRedirect = $session->get('my.redirection')
        && $success == true
        && $isnew == true)
    {
        // Clear the session entry
        $session->set('my.redirection', null);

        JFactory::getApplication()->redirect($finalRedirect);
    }
}
于 2013-10-16T20:27:52.977 回答
0

肯定要归功于坚持下去的@WooDzu。关键肯定是会话变量。但我所做的是创建一个组件来做一件简单的事情。基于带有查询字符串的 url 创建会话变量。所以我的组件将通过以下方式访问:

www.mysite.com/index.php?option=com_mycomp&task=registration&plan=<value>

然后我的组件有一个简单的控制器,如下所示:

function registration()
{
    $jinput = JFactory::getApplication()->input;
    $plan = $jinput->get('plan', null, 'STRING');

    $session = JFactory::getSession();
    $session->set('plan', $plan);

    if ($plan == "page1")
    {
        JFactory::getApplication()->redirect(JRoute::_('index.php?option=com_users&view=registration&plan=page1'));
    }
    else if ($plan == "page2")
    {
        JFactory::getApplication()->redirect(JRoute::_('index.php?option=com_users&view=registration&plan=page2'));

    }
    else 
    {
        JFactory::getApplication()->redirect(JRoute::_('index.php?option=com_users&view=registration&plan=page3'));
    }

}

这将重定向到带有会话变量集的 com_users 注册组件。然后我有了我的插件,OnUserAfterSave但它基于会话变量而不是菜单 ID 或别名进行重定向。

不知道这是否是最好的方法,但它看起来确实很干净。

于 2013-10-22T20:08:11.733 回答