4

我正在创建一个基于 PrestaShop (v 1.6) 的商店,并且我想注册我的自定义修改器插件,以便可以从任何模板(包括前台和后台模板)访问。

问题是在哪里放置注册码?

现在我使用工具覆盖来添加处理修饰符的函数(在商店特定功能的情况下这是可接受的做法,afaik),并使用smarty.config.inc.php来注册插件(因为所有 PrestaShop 插件都是 registereg 在这里),但是这个文件包含一个关于“不要直接修改文件”的警告,据我所知,当我升级 PrestaShop 时会被覆盖。

所以,问题是在哪里注册我的插件以确保我的代码不会被覆盖?

提前致谢。

4

1 回答 1

5

你可以用一个模块来做到这一点。

1.创建一个模块

在 modules 文件夹中创建一个文件夹testmodule并在里面创建一个 php 文件testmodule.php

我们将使用一个actionDispatcher在每个页面控制器实例化之后执行的钩子来将修改器插件注册到 smarty。

require_once _PS_MODULE_DIR_ . 'testmodule' . DIRECTORY_SEPARATOR . 'TestClass.php';

class TestModule extends Module {
    public function __construct()
    {
        $this->name = 'testmodule';
        $this->tab = 'front_office_features';
        $this->version = '1.0';

        parent::__construct();

        $this->displayName = $this->l('Test Module');
        $this->description = $this->l('Testing smarty plugins.');
    }

    public function install()
    {
        return parent::install() && $this->registerHook('actionDispatcher');
    }

    public function hookActionDispatcher()
    {
        /* 
           We register the plugin everytime a controller is instantiated

           'modifier'                          - modifier type of plugin
           'testToUpper'                       - plugin tag name to be used in templates,
           array('TestClass', 'toUpperMethod') - execute toUpperMethod() from class TestClass when using modifier tag name
        */
        $this->context->smarty->registerPlugin('modifier', 'testToUpper', array('TestClass', 'toUpperMethod'));
    }
}

2. 创建一个包含修饰符方法的类

在同一个模块文件夹中创建一个文件TestClass.php. 在其中我们将编写一个静态方法来在调用 smarty 插件时执行。对于这个简单的测试,我们将修改我们想要大写的任何字符串。

class TestClass {
    public static function toUpperMethod($param)
    {
        return strtoupper($param);
    }
}

安装模块,您可以在任何模板中使用您的插件,例如在首页

{$page_name|testToUpper} 

将回显并将页面名称转换为大写。

例如,如果您尝试在数组上使用修饰符,您可以进行修改或保护,但这是注册 smarty 插件的基础知识。

无需覆盖,也无需核心黑客攻击。

于 2016-10-11T19:36:07.137 回答