Magento从那里重定向到页面

Magento从那里重定向到页面

问题描述:

I have a module which had instaled a CMS page with some default URL and some text with form. On this form I have fields and submit. I validate my fields using ajax(send POST in controller of my module). If validation is OK, I redirect to the same page with "succes message".

The problem is, that the default URL of this CMS page in which I make redirect after succes submit can be changed in BO - that's why I cannot just $this->_redirect('default_URL') in my controller, because this url can be changed.

What should I do?

EDIT : Solution: use $this->_redirectReferer() in my controller after success validation

我有一个模块,其中包含一个带有一些默认URL和一些带有表单的文本的CMS页面。 在这个表格上我有字段并提交。 我使用ajax验证我的字段(在我的模块的控制器中发送POST)。 如果验证没问题,我会使用“succes message”重定向到同一页面。 p>

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

我该怎么办? p>

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

Try to put "back URL" to your form as a hidden field or you can use $this->_redirectReferer() to redirect back to CMS page.

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

If you want to redirect to some certain CMS page (not the one where form is placed) you can do this:

  1. Add dropdown to system configuration to be able to select "Success page" (much better than hardcode cms page ID)

  2. Redirect to this page in your controller

Code:

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

You can get a store config by using the getStoreConfig method, something like

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

See Alan's blog, about detailed instructions how to create custom config values (which you might already have) and to access them.

Use this code for redirect to referer

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

For example in custom login form:

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