在 Controller 中传递来自 preDispatch 的 post 数据

Pass post data from preDispatch in Controller

我在购物车页面上有一个自定义表单,用户可以在其中输入特殊代码。为了确保用户已登录,我在控制器中包含了一个 preDispatch 方法。

public function preDispatch() {
    parent::preDispatch();

    $action = $this->getRequest()->getActionName();
    $loginUrl = Mage::helper('customer')->getLoginUrl();

    if (!Mage::getSingleton('customer/session')->authenticate($this, $loginUrl)) {
        $this->_getCoreSession()->addSuccess('Sie müssen sich einloggen oder ein Benutzerkonto erstellen um Ihren Gutschein zu aktivieren.');
        $this->setFlag('', self::FLAG_NO_DISPATCH, true);
    }
}

preDispatch 正在运行。当用户输入代码但未登录时,他将被重定向到登录页面,并在成功登录后返回购物车。 现在处理表单值 post 的函数没有得到输入。 我正在按以下方式检索输入:

public function activateGiftCardCreditAction() {
    // action handles post request when customer activates gift card

    $giftCardCode = trim((string)$this->getRequest()->getParam('giftcardcredit_code'));
    $amount = (float)$this->getRequest()->getParam('giftcard_amount');
    $card = Mage::getModel('giftcards/giftcards')->load($giftCardCode, 'card_code');

    // do something with form values ...

    $this->_redirect('checkout/cart');
}

如何通过登录页面传递表单值?

作为在 会话变量 中保存 post 数据的想法 preDispatch()

    Mage::getSingleton('customer/session')->
setBeforeGiftCardRequest($this->getRequest()->getPost());

然后 post 来自该会话变量的数据

    /* add this code */
     $session = Mage::getSingleton('customer/session');
    $requestParams = $this->getRequest()->getParams();
    if ($session->getBeforeGiftCardRequest()) {
        $requestParams = $session->getBeforeGiftCardRequest();
        $session->unsBeforeGiftCardRequest();
    }

所以让我们试试:

 public function preDispatch() {
    parent::preDispatch();

    $action = $this->getRequest()->getActionName();
    $loginUrl = Mage::helper('customer')->getLoginUrl();

    if (!Mage::getSingleton('customer/session')->authenticate($this, $loginUrl)) {
        $this->_getCoreSession()->addSuccess('Sie müssen sich einloggen oder ein Benutzerkonto erstellen um Ihren Gutschein zu aktivieren.');
  // Assign post value in customer/session variable 
   Mage::getSingleton('customer/session')->setBeforeGiftCardRequest($this->getRequest()->getParams());
        $this->setFlag('', self::FLAG_NO_DISPATCH, true);
    }
}

public function activateGiftCardCreditAction() {
 
    /* add this code */
    $session = Mage::getSingleton('customer/session');
            $requestParams = $this->getRequest()->getParams();
            if ($session->getBeforeGiftCardRequest()) {
                $requestParams = $session->getBeforeGiftCardRequest();
                $session->unsBeforeGiftCardRequest();
            }
    // action handles post request when customer activates gift card

    $giftCardCode = trim((string)$this->getRequest()->getParam('giftcardcredit_code'));
    $amount = (float)$this->getRequest()->getParam('giftcard_amount');
    $card = Mage::getModel('giftcards/giftcards')->load($giftCardCode, 'card_code');

    // do something with form values ...

    $this->_redirect('checkout/cart');
}