prestashop自定义钩子模块管理控制器

时间:2018-10-18 09:14:24

标签: prestashop-1.6

我目前正在开发模块prestashop,但遇到问题。我为模块创建了一个新钩子,并在模块中添加了一个执行该钩子的管理控制器,并添加了调用该钩子并使用模板的函数。除了没有显示模板之外,我都有一个空白页面。这是我做的代码:

我的模块:

<?php

if (!defined('_PS_VERSION_'))
    exit;
/* Checking compatibility with older PrestaShop and fixing it */
if (!defined('_MYSQL_ENGINE_'))
    define('_MYSQL_ENGINE_', 'MyISAM');

require_once(_PS_MODULE_DIR_.'blockobjectif/classes/Objectif.php');

class blockobjectif extends Module
{
    public function __construct()
    {
        $this->name = 'blockobjectif';
        $this->tab = 'front_office_features';
        $this->version = '1.0';
        $this->author = 'Athor Athor';
        $this->bootstrap = true;
        $this->need_instance = 0;
        $this->ps_versions_compliancy['min'] = '1.5';
        $this->ps_versions_compliancy['max'] = '1.6';

        parent::__construct();

        $this->displayName = $this->l('Objectifs');
        $this->description = $this->l('Définie des objectifs aux clients');
    }

    public function install()
    {
        $sql = array();
        include(dirname(__FILE__).'/sql/install.php');
        foreach ($sql as $s)
            if (!Db::getInstance()->execute($s))
                return false;

        $class = 'AdminObjectif';
        $tab = new Tab();
        $tab->class_name = $class;
        $tab->module = $this->name;
        $tab->id_parent = (int) Tab::getIdFromClassName('AdminParentCustomer');
        $langs = Language::getLanguages(false);
        foreach ($langs as $l) {
            $tab->name[$l['id_lang']] = $this->l('Objectifs');
        }
        $tab->save();

        return parent::install()
            && $this->registerHook('displayCustomerAccount')
            && $this->registerHook('displayAdminObjectifs');
    }

    public function uninstall($delete_params = true)
    {
        $sql = array();
        include(dirname(__FILE__).'/sql/uninstall.php');
        foreach ($sql as $s)
            if (!Db::getInstance()->execute($s))
                return false;

        $moduleTabs = Tab::getCollectionFromModule($this->name);
        if (!empty($moduleTabs)) {
            foreach ($moduleTabs as $moduleTab) {
                $moduleTab->delete();
            }
        }

        if (!parent::uninstall())
            return false;

        return true;
    }

    public function hookDisplayCustomerAccount($params)
    {
        ddd($params);
    }

    public function hookDisplayAdminObjectifs($params)
    {
        return $this->display(__FILE__, 'admin-obj.tpl');
    }
}

我的AdminObjectifController:

<?php

class AdminObjectifController extends ModuleAdminController
{
    public function __construct()
    {
        $this->bootstrap = true;
        $this->table = 'objectifs';
        $this->className = 'Objectif';

        parent::__construct();

        Hook::exec('displayAdminProductsExtra');
    }
}

结果: objectifs

我看不出问题出在哪里...

谢谢您的帮助

1 个答案:

答案 0 :(得分:1)

这不是显示后台控制器模板的正确方法。

这是您应该尝试的方法:

<?php

class AdminObjectifController extends ModuleAdminController
{
    public function __construct()
    {
        $this->bootstrap = true;
        $this->table = 'objectifs';
        $this->className = 'Objectif';

        parent::__construct();
    }

    public function initContent()
    {
        $tpl = $this->context->smarty->createTemplate($this->getTemplatePath() . 'admin-obj.tpl', $this->context->smarty);


        $tpl->assign(array(
            'my_var' => "test"
        ));

        $this->content .= $tpl->fetch();
        parent::initContent();
    }
}

请注意,您的admin-obj.tpl文件应放在模块内部的views/templates/admin/admin-obj.tpl下。