我有许多指向用户注册组件的菜单。根据用户注册的位置,我使用自定义插件相应地重定向它们。
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
}
}
根据此插件中分配的菜单,我将用户重定向到特定页面。这一切都很有效。
我的问题是当注册失败时(即重复的用户名,错误的匹配密码等),页面会因服务器端验证而刷新。此刷新使页面远离原始链接。它似乎重定向到我创建的第一个注册菜单页面。
例如:我在注册页面上输入了错误的凭据,然后页面刷新到完全不同的页面。有任何想法吗?我想AJAX注册的扩展可能会解决这个问题,但我现在想避免这种情况。
答案 0 :(得分:1)
您可以执行以下操作:
我想不出更简单的做法或任何并发症。
<?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);
}
}
答案 1 :(得分: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或别名。
不知道这是否是最佳方法,但看起来确实相当干净。