PrestaShop $this->context->smarty 为空

PrestaShop $this->context->smarty is null

我正在制作 PrestaShop 1.6 模块,但发生了一些奇怪的事情。在此模块的配置页面中,我为用户提供了两种形式。一个用于配置自动电子邮件,另一个用于测试电子邮件。我的问题是,在提交第二个表单后,$this->context->smarty 为空,给我这个错误:

Fatal error: Call to a member function assign() on null in /Users/andre/Projects/Web/xxx/modules/closecustomerthreademail/closecustomerthreademail.php on line 90

在第 90 行中,我有:$this->context->smarty->assign('module_dir', $this->_path); 位于此函数内部:

public function getContent()
{
    /**
     * If values have been submitted in the form, process.
     */
    if (((bool)Tools::isSubmit('submitClosecustomerthreademailModule')) == true) {
        $this->postProcess();
    }

    $this->context->smarty->assign('module_dir', $this->_path);
    $output = $this->context->smarty->fetch($this->local_path.'views/templates/admin/configure.tpl');

    return $output.$this->renderForm();
}

这是我在屏幕中添加两个表单的方式(renderForm 方法):return $helper->generateForm(array($this->getConfigForm(), $this->getTestForm()));

后处理。抱歉粘贴了这么多代码,我正在尝试提供我认为相关的所有详细信息。

protected function postProcess()
{
    if(Tools::isSubmit('test_form')) {
        $this->sendTestEmailTo(Tools::getValue('CLOSECUSTOMERTHREADEMAIL_EMAIL_TO_TEST'));
    }
    elseif(Tools::isSubmit('config_form')) {

        $form_values = $this->getConfigFormValues('config_form');

        foreach (array_keys($form_values) as $key) {
            Configuration::updateValue($key, Tools::getValue($key));
        }
    }
}

sendTestEmailTo只是有一个IF语句,然后调用这个函数:

public function sendEmail($to_email, $to_name = null){

    $id_lang = $this->context->language->id;
    $template_dir = _PS_MODULE_DIR_.$this->name.'/views/templates/email/';

    $vars = array(
        '{html}' => Configuration::get('CLOSECUSTOMERTHREADEMAIL_EMAIL_HTML_BODY'),
        '{text}' => Configuration::get('CLOSECUSTOMERTHREADEMAIL_EMAIL_TEXT_BODY')
    );

    require(_PS_CONFIG_DIR_.'config.inc.php');
    require_once(_PS_ROOT_DIR_.'/init.php');

    $send = Mail::Send(
        (int)$id_lang,
        'email',
        Configuration::get('CLOSECUSTOMERTHREADEMAIL_EMAIL_SUBJECT'),
        $vars,
        $to_email,
        $to_name,
        null,
        null,
        null,
        null,
        $template_dir,
        false,
        (int)$this->context->shop->id,
        null
    );

    return $send;
}

我没看到是什么导致 $this->context->smarty 变成 null。您有什么提示可以帮助我进行调查吗?

编辑

构造方法:

public function __construct()
{
    $this->name = 'closecustomerthreademail';
    $this->tab = 'others';
    $this->version = '1.0.0';
    $this->author = 'andre';
    $this->need_instance = 0;

    /**
     * Set $this->bootstrap to true if your module is compliant with bootstrap (PrestaShop 1.6)
     */
    $this->bootstrap = true;

    parent::__construct();

    $this->displayName = $this->l('Close Customer Thread e-mail');
    $this->description = $this->l('Sends an e-mail whenever you close a customer thread');

    $this->confirmUninstall = $this->l('Are you sure you want to uninstall this module?');

    $this->ps_versions_compliancy = array('min' => '1.6', 'max' => _PS_VERSION_);
}

我在发货前想不通,所以我不得不这样做来解决,一个重定向:

protected function postProcess()
{
    if(Tools::isSubmit('test_form')) {
        $this->sendTestEmailTo(Tools::getValue('CLOSECUSTOMERTHREADEMAIL_EMAIL_TO_TEST'));
    }
    elseif(Tools::isSubmit('config_form')) {

        $form_values = $this->getConfigFormValues('config_form');

        foreach (array_keys($form_values) as $key) {
            Configuration::updateValue($key, Tools::getValue($key));
        }
    }

    $actual_link = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

    Tools::redirectAdmin($actual_link);
}

很高兴您解决了问题。但我想问题是这样的:

require(_PS_CONFIG_DIR_.'config.inc.php');
require_once(_PS_ROOT_DIR_.'/init.php');

删除这些行 :),你永远不应该在 class 方法中包含这些文件。