magento自定义模块控制器无法获取toHtml()函数

发布于 2024-11-04 16:46:18 字数 2236 浏览 0 评论 0原文

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

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 中有一个块,并且有一个 toHtml()。或者错误的原因应该是什么?

请求

这是我的重定向块,位于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;

    }
}

My custom module is a payment gateaway, i have a redirect controller and here is the script

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

}

The function redirectAction() can't get the toHtml() function

and here is the error:

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

what should be the error of this? I have a block in ModuleName/Block/Redirect.php and I have a toHtml(). or what should be the cause of error?

REQUESTED

Here is my redirect block located at 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;

    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

为你鎻心 2024-11-11 16:46:18
$this->loadLayout();

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

$this->loadLayout();

At that point blocks are not loaded yet. You have to call load layout and only after you will be able to access blocks defined in layout.

看轻我的陪伴 2024-11-11 16:46:18

这是给你带来问题的行

$this->getLayout()->createBlock('asurepay/redirect')->toHtml()

让我们使用非链式语法重写它,让事情变得更清楚

$layout = $this->getLayout();
$block  = $layout->createBlock('asurepay/redirect');
$html   = $block->toHtml();

当 PHP 抱怨时,

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

它说 $block 变量(调用 >createBlock) 不是一个对象。这意味着您对 createBlock 的调用由于某种原因失败了。

我有根据的猜测是您的 config.xml 文件设置不正确。根据您的块类名称 (Asurepay_Custompay_Block_Standard_Redirect) 和 config.xml 中的标准约定,您的别名应该是

$layout->createBlock('custompay/standard_redirect');

但是,假定 config.xml设置类似的内容

<config>
    <!-- ... -->
    <global>
        <blocks>
            <custompay>
                <class>Asurepay_Custompay_Block</class>
            </custompay>                
        </blocks>
    </global>
    <!-- ... -->
</config>

如果您发布 config.xml 的内容,人们将能够诊断您的问题。

Here's the line that's causing you a problem

$this->getLayout()->createBlock('asurepay/redirect')->toHtml()

Let's rewrite that using non-chained syntax to take things a little me clear

$layout = $this->getLayout();
$block  = $layout->createBlock('asurepay/redirect');
$html   = $block->toHtml();

When PHP complains with

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

It's saying that the $block variable (the results of the call to createBlock) is not an object. That means your call to createBlock has failed for some reason.

My educated guess here is you're config.xml file is setup incorrectly. Based on your block class name (Asurepay_Custompay_Block_Standard_Redirect) and standard conventions in config.xml, your alias should be

$layout->createBlock('custompay/standard_redirect');

However, that assumes a config.xml setup something like

<config>
    <!-- ... -->
    <global>
        <blocks>
            <custompay>
                <class>Asurepay_Custompay_Block</class>
            </custompay>                
        </blocks>
    </global>
    <!-- ... -->
</config>

If you post the contents of your config.xml people will be able to diagnose your problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文