【问题标题】:Magento: Can not change value of Mage::getSingleton(’core/session’)?Magento:不能改变 Mage::getSingleton('core/session') 的值?
【发布时间】:2013-10-15 23:15:21
【问题描述】:

我有一个扩展

在控制器 php 文件中,我在下面有一行

public function accountverificationAction() {
        $customerEmail = Mage::getSingleton('customer/session')->getCustomer()->getEmail();
        $mobile_num = $this->getRequest()->getParam('mobile_number');
        $mobile_code = $this->getRequest()->getParam('mobile_code');        
        $smsModel = Mage::getModel('ardindata/ardindata');

        Mage::getSingleton('core/session')->setVerificationphase('mobile');

        if($mobile_num) {
            $sms_response = $smsModel->sendSms($mobile_num,$customerEmail);
            if(is_numeric($sms_response)) {
                Mage::getSingleton('core/session')->setVerificationphase('code');
            } else {
                if(empty($sms_response)) {
                    $message = $this->__('Error: Please enter a valid mobile number');
                } else {
                    $message = $this->__('Error: '.$sms_response);
                }
                Mage::getSingleton('core/session')->addError($message);
            }
        }

    if($mobile_code) {
        $verificationCompleted = $smsModel->customerVerification($customerEmail,$mobile_code);
        if($verificationCompleted != '1') {
            $message = $this->__('Verification Failed!');
            Mage::getSingleton('core/session')->addError($message);
        }
    }

    $this->loadLayout();
    $this->renderLayout();
}

所以,我首先将值 "mobile" 设置为 Mage::getSingleton('core/session') ...我想将其更改为 "code " 在 if(is_numeric($sms_response)) 之后,但它不会改变并仅回显“mobile”!

不知道为什么,谁能帮我解决一下?

谢谢大家

【问题讨论】:

    标签: php magento mage


    【解决方案1】:

    在为核心/会话变量分配新值之前,您需要取消设置旧的分配值。

    修正后的完整函数代码:

    public function accountverificationAction() {
        $customerEmail = Mage::getSingleton('customer/session')->getCustomer()->getEmail();
        $mobile_num = $this->getRequest()->getParam('mobile_number');
        $mobile_code = $this->getRequest()->getParam('mobile_code');        
        $smsModel = Mage::getModel('ardindata/ardindata');
    
        Mage::getSingleton('core/session')->setVerificationphase('mobile');
    
        if($mobile_num) {
            $sms_response = $smsModel->sendSms($mobile_num,$customerEmail);
            if($sms_response) {
                Mage::getSingleton('core/session')->unsVerificationphase();
                Mage::getSingleton('core/session')->setVerificationphase('code');
            } else {
                if(empty($sms_response)) {
                    $message = $this->__('Error: Please enter a valid mobile number');
                } else {
                    $message = $this->__('Error: '.$sms_response);
                }
                Mage::getSingleton('core/session')->addError($message);
            }
        }
    

    【讨论】:

    • 之前做过测试,但是不行! --- 似乎值不能超出 if !!
    • 核心/会话变量是全局变量,不限于范围,尝试在取消设置之前打印Mage::getSingleton('core/session')->getVerificationphase();,并将新值设置到会话变量中。
    • 我在模板文件中设置了echo Mage::getSingleton('core/session')->getVerificationphase();,在控制器文件中Mage::getSingleton('core/session')->setVerificationphase('code');之后设置了echo Mage::getSingleton('core/session')->getVerificationphase();...但是不知道为什么模板文件中的“code”可以不返回,所有时间都是“移动的”!--在模板中:$verification_failure = Mage::getSingleton('core/session')->getVerificationphase(); echo $verification_failure; if(empty($verification_failure) || ($verification_failure == 'mobile')):
    • 这意味着,if(is_numeric($sms_response)) 不正确,它不会取消设置/更新会话变量。或者您可以使用if($sms_response) 代码行更新条件。然后添加未设置/设置会话变量。
    • 不能再工作了,if(is_numeric($sms_response)) 是真的,因为发送短信时返回“代码”但不显示在模板文件中,当我在控制器文件中的 Mage::getSingleton('core/session')->setVerificationphase('code'); 之后回显 Mage::getSingleton('core/session')->getVerificationphase(); 然后“ code”出现在模板文件中的“mobile”下,不要传递给else!我不知道我该如何解释更多
    猜你喜欢
    • 1970-01-01
    • 2014-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多