magento自定义模块控制器无法获取toHtml()函数
我的自定义模块是支付网关,我有一个重定向控制器,这里是脚本
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
此时块尚未加载。您必须调用加载布局,只有在您才能访问布局中定义的块之后。
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.
这是给你带来问题的行
让我们使用非链式语法重写它,让事情变得更清楚
当 PHP 抱怨时,
它说
$block
变量(调用>createBlock
) 不是一个对象。这意味着您对createBlock
的调用由于某种原因失败了。我有根据的猜测是您的 config.xml 文件设置不正确。根据您的块类名称 (
Asurepay_Custompay_Block_Standard_Redirect
) 和config.xml
中的标准约定,您的别名应该是但是,假定
config.xml
设置类似的内容如果您发布
config.xml
的内容,人们将能够诊断您的问题。Here's the line that's causing you a problem
Let's rewrite that using non-chained syntax to take things a little me clear
When PHP complains with
It's saying that the
$block
variable (the results of the call tocreateBlock
) is not an object. That means your call tocreateBlock
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 inconfig.xml
, your alias should beHowever, that assumes a
config.xml
setup something likeIf you post the contents of your
config.xml
people will be able to diagnose your problem.