Magento重定向到页面从那来


Magento redirect to the page from that came

我有一个模块,它安装了一个带有一些默认URL和一些带有表单的文本的CMS页面。在此表格上,我有字段并提交。我使用 ajax(在我的模块的控制器中发送 POST)验证我的字段。如果验证正常,我会重定向到带有"成功消息"的同一页面。

问题是,我可以在成功提交后进行重定向的此CMS页面的默认URL可以在BO中更改-这就是为什么我不能只$this->_redirect('default_URL')控制器的原因,因为可以更改此URL。

我该怎么办?

编辑:解决方案:成功验证后在我的控制器中使用 $this->_redirectReferer()

尝试将表单中的"返回URL"作为隐藏字段,或者您可以使用 $this->_redirectReferer() 重定向回 CMS 页面。

见 http://docs.magentocommerce.com/Mage_Core/Mage_Core_Controller_Varien_Action.html#method_redirectReferer

如果要重定向到某个特定的CMS页面(不是放置表单的页面),则可以执行以下操作:

  1. 将下拉列表添加到系统配置中,以便能够选择"成功页面"(比硬编码cms页面ID好得多)

  2. 重定向到控制器中的此页面

法典:

$pageId = Mage::getStoreConfig('mymodule/config/success_page');
$page = Mage::getModel('cms/page')->load($pageId);
$this->_redirect($page->getUrlKey());

您可以使用 getStoreConfig 方法获取存储配置,如下所示

Mage::getStoreConfig('helloworld_options/messages/hello_message');

请参阅 Alan 的博客,了解如何创建自定义配置值(您可能已经拥有)和访问它们的详细说明。

使用此代码重定向到引用

<?php Mage::getSingleton('customer/session')->setBeforeAuthUrl($this->getRequest()->getRequestUri());?>

例如,在自定义登录表单中:

<form method="post" action="<?php echo Mage::helper('customer')->getLoginPostUrl() ?>">
    <?php Mage::getSingleton('customer/session')->setBeforeAuthUrl($this->getRequest()->getRequestUri());?>
...
...