Adn*_*nan 10 php opencart opencart2.x opencart-events
我已经搜索了很多关于opencart触发器但没有找到合适的例子.在opencart 2.0中,有一些触发器,开发人员可以在其中挂钩函数并执行类似wordpress操作和过滤器的操作.例如在
catalog/model/checkout/order.php
Run Code Online (Sandbox Code Playgroud)
有一个触发器 $this->event->trigger('post.order.history.add', $order_id)
有人可以帮助我在上面的触发器上挂钩我的功能吗?
Dmi*_*huk 10
我认为这个问题应该更新,因为 OpenCart v2.3.x OC 事件的更新发生了巨大变化。
在它们类似于 Wordpress 方法之前。您将有一个带有before和after触发器的预定义事件列表。
从 2.3.x 版本开始,然后在 3.x 版本中发生了一些变化,OC 事件变得更加动态,其中触发器根据文件的路径结构动态定义。
当然,也有一些缺点:
我建议在可能的情况下使用事件,并回退到事件无法访问的 OCmod。
当控制器(模型、视图、库或语言)通过加载器类加载时,如下所示:
$this->load->controller('common/header');
Run Code Online (Sandbox Code Playgroud)
controller()文件system/engine/loader.php中的方法执行以下操作
before路由事件common/header$trigger = $route;
$result = $this->registry->get('event')->trigger('controller/' . $trigger . '/before', array(&$route, &$data));
Run Code Online (Sandbox Code Playgroud)
$action = new Action($route);
$output = $action->execute($this->registry, array(&$data));
Run Code Online (Sandbox Code Playgroud)
after路由事件common/header$result = $this->registry->get('event')->trigger('controller/' . $trigger . '/after', array(&$route, &$data, &$output));
Run Code Online (Sandbox Code Playgroud)
这对于模型、语言、库和配置来说非常相似。
OpenCart 将当前加载的控制器路由(就像我们上面加载的那个)common/header(模型、视图、语言和配置一样)与系统中注册的事件相匹配。
您将在注册事件时添加触发器。
对于
common/home加载前目录中的控制器
触发器将如下所示:
catalog/controller/common/home/before
对于它加载的目录中
sale/order方法的模型getOrderafter
触发器将如下所示:
catalog/model/sale/order/getOrder/after
重要的是,在模型中,您将始终为方法设置触发器,这与控制器不同,那里有一个
index经常被删除的默认方法对于
catalog/product_form.twig在管理员中查看之后
触发器将如下所示:
admin/view/catalog/product_form/after
请记住,在之后向视图添加事件时,您将修改 &$output veriable,它现在不是树枝文件,而是完全编译的 HTML。所以代码中不再有树枝标签了。
使用PHP Simple HTML Dom Parser修改视图的最佳方法
有三个选项可以注册 OpenCart 事件。
system/config/catalog编辑中的配置文件注册$_['action_event']我将向您展示如何通过模型添加,因为这是推荐的方式。
在你的文件中 admin/controller/extension/module/my_module.php
$this->load->model('setting/event');
$this->model_setting_event->deleteEventByCode('my_module');
if($this->request->post['module_my_module_status']){
$this->model_setting_event->addEvent('my_module',
'admin/view/catalog/product_form/before',
'extension/module/my_module/view_catalog_product_form_before');
}
Run Code Online (Sandbox Code Playgroud)
请注意,我首先删除所有事件,然后检查状态是否为真并添加事件。通过这种方式,我可以确保不会添加旧的或意外的事件。
public function uninstall(){
$this->load->model('setting/event');
$this->model_setting_event->deleteEventByCode('my_module');
}
Run Code Online (Sandbox Code Playgroud)
如果模块被卸载,最好在模块之后删除所有事件。
现在您知道有些事件具有触发器。但是触发器实际上做了什么?
他们只是加载事件的控制器并通过引用传递数据。
这就是为什么:
before事件和$路径和$输出after事件这是文件中事件函数的示例 admin/controller/extension/module/my_module.php
public function view_catalog_product_form_before(&$route, &$data){
if($data && isset($this->request->get['product_id'])){
$product_id = $this->request->get['product_id'];
$this->load->model('extension/module/my_module');
$product_info = $this->model_extension_module_my_module->getMoreProductInfo($product_id);
if($product_info){
//here I am modifying the $data and it will pass on to the view with this new data because it was passed to this function by referance.
$data['more_info'] = $product_info;
}
}
//there is no result returned in Event functions.
//if you do return a result, you will override the default result that can cause unexpected consequences. best to avoid it.
}
Run Code Online (Sandbox Code Playgroud)
享受!如果您有任何问题,请通过https://dreamvention.com给我们发送消息
重要说明:此答案适用于OC> 2.0.xx和<2.2.xx
这里的问题是使用(和搜索)一个错误的单词 - 你应该搜索的正确的是事件,并从它派生事件监听器和触发事件(不幸的是,当试图搜索那些和2.0的文档仍然缺失).
现在我相信整个背景更容易理解,特别是如果你对其他框架中的事件有一些了解(也许是jQuery?),这里只是一个如何处理事件的快速指南(在OC 2.0中):
首先我们需要注册一个事件监听器,如下所示:
$this->event->register('post.order.history.add', 'checkout/order/send_email');
在某些地方触发事件,例如
$this->event->trigger('pre.order.history.add', $order_id);
和
$this->event->trigger('post.order.history.add', $order_id);
如果事件(由其名称标识post.order.history.add)侦听器已注册,则将在触发器上调用
有关更多信息或自行解决,您可能需要了解一下system/engine/event.php(现在没有其他工作可做).
小智 5
重要说明:此答案适用于OC> 2.0.xx和<2.2.xx
事件系统的工作方式如下:
您可以使用$ event对象在运行时注册事件处理程序或触发事件,但仅在特殊情况下执行此操作.请记住,您很可能需要通过$ this-> event而不是$ event(取决于您需要它的位置)来访问$ event对象.
通常,您只需使用扩展/事件模型在db表中注册事件处理程序一次.例如,您可以在管理控制器的install()方法中执行此操作.像这样的东西:
public function install() {
$this->load->model('extension/event');
$this->model_extension_event->addEvent('mymodule', 'pre.admin.store.delete', 'module/mymodule/on_store_delete');
$this->model_extension_event->addEvent('mymodule', 'post.customer.add', 'module/mymodule/on_customer_add');
}
Run Code Online (Sandbox Code Playgroud)
事件处理程序是addEvent()方法的第三个参数,它们采用标准路由的形式.
您可以在此处找到有关事件系统的更多信息:http://isenselabs.com/posts/opencart2-event-system-tutorial.它是一个教程,解释了事件系统的工作原理,并提供了一些简单的示例,向您展示如何在扩展中使用它.