php - magento自定义模块 Controller 无法获取toHtml()函数

标签 php zend-framework magento

我的自定义模块是支付网关,我有一个重定向 Controller ,这是脚本

class Asurepay_Custompay_AsurepayController extends Mage_Core_Controller_Front_Action {

    protected $_order;

    public function getOrder() {
        if ($this->_order == null) {

        }
        return $this->_order;
    }

    public function indexAction(){
        echo "index test 1";
    }

    public function redirectAction(){
        $session = Mage::getSingleton('checkout/session');
        $session->setAsurepayCustompayQuoteId($session->getQuoteId());
        $this->getResponse()->setBody($this->getLayout()->createBlock('asurepay/redirect')->toHtml());
        $session->unsQuoteId();
        $session->unsRedirectUrl();
    }

}

函数redirectAction()无法获取toHtml()函数

这是错误:

Fatal error: Call to a member function toHtml() on a non-object in my code

这应该是什么错误?我在 ModuleName/Block/Redirect.php 中有一个 block ,并且有一个 toHtml()。或者错误的原因是什么?

请求

这是我的重定向 block ,位于 ModuleName/Block/Redirect.php

class Asurepay_Custompay_Block_Standard_Redirect extends Mage_Core_Block_Abstract {

    protected function _toHtml() {

        $asure = Mage::getModel("custompay/asure");
        $form = new Varien_Data_Form();

        $form = new Varien_Data_Form();
        $form->setAction($standard->getConfig()->getGateurl)
            ->setId('asurepay_custompay_checkout')
            ->setName('asurepay_custompay_checkout')
            ->setMethod('POST')
            ->setUseContainer(true);

        foreach ($standard->getStandardCheckoutFormFields() as $field=>$value) {
            $form->addField($field, 'hidden', array('name'=>$field, 'value'=>$value, 'size'=>200));
        }
        $html = '<html><body>';
        $html.= $this->__('You will be redirected to AsurePay in a few seconds.');
        $html.= $form->toHtml();
        $html.= '<script type="text/javascript">document.getElementById("asurepay_checkout").submit();</script>';
        $html.= '</body></html>';
        return $html;

    }
}

最佳答案

$this->loadLayout();

此时 block 尚未加载。您必须调用加载布局,只有在您才能访问布局中定义的 block 之后。

关于php - magento自定义模块 Controller 无法获取toHtml()函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5855019/

相关文章:

php - laravel 4 - 如何为特定方法加载不同的布局?

Php Phalcon - Select 语句不返回任何内容

php - 左连接查询中两列的连接

php - zend框架FlashMessenger问题

php - mdate() 函数 codeigniter - 错误

zend-framework - 使用 Zend_Form 登录的典型问题

java - 如何配置 Magento Enterpise 以使用 Solr 作为主要搜索引擎?

javascript - 带缩略图的 jQuery fancybox

php - 无法从 Magento 产品集合中检索价格信息

php - 如何使用 `created_at` 列获取最新数据