【问题标题】:Pass post data from preDispatch in Controller在 Controller 中传递来自 preDispatch 的 post 数据
【发布时间】:2015-09-24 13:49:22
【问题描述】:

我在购物车页面上有一个自定义表单,用户可以在其中输入特殊代码。为了确保用户已登录,我在控制器中包含了一个 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 正在运行。当用户输入代码但未登录时,他将被重定向到登录页面,并在成功登录后返回购物车。 现在处理表单值发布的函数没有得到输入。 我正在检索输入如下:

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');
}

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

【问题讨论】:

    标签: php magento controller checkout magento-1.9


    【解决方案1】:

    作为将帖子数据保存在 会话变量 preDispatch()

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

    然后从该会话变量发布数据

        /* 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');
    }

    【讨论】:

    • 感谢您的提示。您的解决方案只需稍作修正即可。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多