我正在创建一个基于 PrestaShop(v 1.6)的商店,并且我想注册我的自定义修改器插件,以便可以从任何模板(包括前台和后台模板)访问。
问题是where放置注册码?
现在我用一个Tools class override添加函数来处理修饰符(据我所知,在商店特定功能的情况下这是可接受的做法),以及smarty.config.inc.php注册插件(因为所有PrestaShop插件都在这里注册),但是这个文件包含一个关于“不要直接修改文件”的警告,据我了解,当我升级PrestaShop时,它将被覆盖。
那么,问题是在哪里注册我的插件以确保我的代码不会被覆盖?
提前致谢。
您可以使用模块来完成此操作。
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 插件的基础知识。
无需覆盖,也无需核心黑客攻击。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)