1

嗨,我是 prestashop 的新手,我尝试为 1.7 创建一个管理模块。我会创建一个新菜单来显示模板并管理我的数据库。

模块/mymodule/mymodule.php:

<?php
if (!defined('_PS_VERSION_'))
{
    exit;
}

class MyModule extends Module
{
    public function __construct()
    {
        $this->name = 'mymodule';
        $this->tab = 'administration';
        $this->version = '1.0.0';
        $this->author = 'John doe';

        $this->bootstrap = true;
        parent::__construct();

        $this->ps_versions_compliancy = array('min' => '1.6', 'max' => _PS_VERSION_);
        $this->displayName = $this->l('Mon module');
        $this->description = $this->l('On test la creation de module presta.');
    }

    public function install()
    {
         // Install Tabs
        $tab = new Tab();
        $tab->active = 1;
        $tab->class_name = "MyModule";
        $tab->module = 'mymodule';
        $tab->name = array();
        $tab->id_parent = (int)Tab::getIdFromClassName('SELL');
        $tab->position = 3;
        foreach ($lang as $l) {
            $tab->name[$l['id_lang']] = $this->l('Mon module');
        }

        $tab->add();

        if (!parent::install())
            return false;
        return true;
    }

    public function uninstall()
    {
        // Uninstall Tabs
        $tab = new Tab((int)Tab::getIdFromClassName('Mymodule'));
        $tab->delete();

        // Uninstall Module
        if (!parent::uninstall())
            return false;
        return true;
    }
}

模块/mymodule/controller/admin/MyModuleController.php:

<?php
class MyModuleController extends ModuleAdminController
{
    public function renderList() {
        $this->content = $this->createTemplate('mymodule.tpl')->fetch();
        return $this->content;
    }
}
?>

模块/mymodule/views/templates/admin/mymodule.tpl:

{block name="page_title"}
    {l s='Mon module'}
{/block}
<section >
    <div>Hello world !</div>
</section>

我通过编译大量教程 1.7 / 1.6 创建了它,但安装失败。Prestashop 为我们提供了一个文档来创建第一个模块,但它并没有真正记录在案,我在互联网上没有发现任何真正有用的 1.7。

有什么帮助/建议吗?

谢谢

EDIT1:好的,安装正确,我的选项卡已创建,但是当我单击它时,它会调用“controller = MyModule”并且找不到模块控制器。就快结束了。

4

2 回答 2

2

模块的install()方法必须返回真/假。

至于模板加载从ModuleAdminController

$this->createTemplate($template); 

尝试查找模板并加载它找到的第一个模板:

  1. /current_theme/modules/yourmodule/views/templates/admin/
  2. /modules/yourmodule/views/templates/admin/

因此,如果您createTemplate()使用路径调用,它会附加到上述两个文件夹中的任何一个,并且找不到您的模板,从而引发错误。

将您的模板移入modules/yourmodule/views/templates/admin/并仅使用模板名称调用 create 方法。

$this->createTemplate('mymodule.tpl');
于 2017-02-02T14:26:38.057 回答
1

您的模块的 install() 函数似乎有问题。您正在注册的钩子似乎也是错误的。试试下面的代码,它对我们有用:

public function install()
    {
        if (!parent::install() ||
                !$this->registerHook('header')) {
            return false;
        }
        if (Shop::isFeatureActive()) {
            Shop::setContext(Shop::CONTEXT_ALL);
        }

        $lang = Language::getLanguages();
        $tab = new Tab();
        $tab->class_name = 'AdminTestimonialSetting';
        $tab->module = 'testimonial';
        $tab->id_parent = 2;
        $tab->position = 6;
        foreach ($lang as $l) {
            $tab->name[$l['id_lang']] = $this->l('Testimonial');
        }

        $tab->save();

        return true;
    }
于 2017-02-02T15:01:01.297 回答