Php PrestaShop后端,ajax调用中出现致命错误
在PrestaShop站点的后端,我正在使用以下功能:Php PrestaShop后端,ajax调用中出现致命错误,php,ajax,prestashop,prestashop-1.6,Php,Ajax,Prestashop,Prestashop 1.6,在PrestaShop站点的后端,我正在使用以下功能: public function hookAjax($action, $id_product, $id_lang, $title, $descript, $order, $id = NULL) { /* various code*/ $this->context->smarty->assign( array( 'block_define' => $this-
public function hookAjax($action, $id_product, $id_lang, $title, $descript, $order, $id = NULL)
{
/* various code*/
$this->context->smarty->assign(
array(
'block_define' => $this->getFormDesc($id_product)
)
);
return $this->context->smarty->fetch($this->local_path.'views/templates/hook/admin_extra_desc.tpl');
}
public function getFormDesc($id_product) {
$array = array();
foreach (Language::getLanguages() as $lang) {
/*various code*/
foreach($result as $k=> $r) {
$files = array();
$helper = new HelperImageUploader();
$helper->setMultiple(false)->setUseAjax(true)->setName('thumbnail_'.$r['id'].'_'.$r['id_lang'])->setFiles($files)->setMaxFiles(3)->setUrl('../modules/module-name/imgAjaxCall.php?');
$result[$k]['img-form'] = $helper->render();
$result[$k]['img'] = $result[$k]['img'] ? _PS_BASE_URL_.__PS_BASE_URI__.'modules/module-name/upload/'.$result[$k]['img'] : '';
}
$array[$lang["id_lang"]] = array(
'lang_data' => $lang,
'count' => count($result),
'data' => $result
);
}
return $array;
}
调用HookAjax的方法是:
<?php
include(dirname(__FILE__).'/../../config/config.inc.php');
$context = Context::getContext();
$addDesc = Module::getInstanceByName('module-name');
echo $addDesc->hookAjax($_POST['action'],$_POST['id_prodotto'],$_POST['lang'],$_POST['title'], $_POST['text_desc'], NULL, $_POST['row']);
?>
但我在与这个错误作斗争:
致命错误:在第257行的{my_site}/classes/helper/HelperUploader.php中的非对象上调用成员函数addJs()在包含
config.inc.php
之后,需要在HookAjax中包含init.php
,以便在上下文中初始化控制器
include(dirname(__FILE__).'/../../init.php');
请注意,这只是一种不好的做法,请尊重MVC并在其中使用适当的控制器进行AJAX调用和数据验证/处理,而不是在主模块类中。可能是因为addDesc来自模块类,不属于控制器类。然后,$this->getContext()->controller->addJs没有“找到”控制器。解决此问题的最佳方法是在模块中创建admincontroller,然后在那里调用函数。