我正在尝试编写一个自定义模块,该模块将挂钩post.order.history.add
事件并执行自定义操作。这是我的模块结构&代码到目前为止:
管理\控制器\模块\ mysite.php
<?php
class ControllerModuleMysite extends Controller
{
private $error;
private $logger;
public function __construct()
{
$this->error = array();
$this->logger = new Log('MySite.log');
}
/* Event Hooks */
public function install()
{
$this->load->model('extension/event');
$this->model_extension_event->addEvent('mysite_NewOrder', 'post.order.history.add', 'module/mysite/on_order_history_add');
}
public function uninstall()
{
$this->load->model('extension/event');
$this->model_extension_event->deleteEvent('mysite_NewOrder');
}
/* Admin Config Handler */
public function index()
{
// Loading the language file of mysite
$this->load->language('module/mysite');
// Set the title of the page to the heading title in the Language file
$this->document->setTitle($this->language->get('heading_title'));
// Load the Setting Model (All of the OpenCart Module & General Settings are saved using this Model )
$this->load->model('setting/setting');
// Start If: Validates and check if data is coming by save (POST) method
if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate())
{
// Parse all the coming data to Setting Model to save it in database.
$this->model_setting_setting->editSetting('mysite', $this->request->post);
// To display the success text on data save
$this->session->data['success'] = $this->language->get('text_success');
// Redirect to the Module Listing
$this->response->redirect($this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL'));
}
// Assign the language data for parsing it to view
$data['heading_title'] = $this->language->get('heading_title');
$data['text_edit'] = $this->language->get('text_edit');
$data['text_enabled'] = $this->language->get('text_enabled');
$data['text_disabled'] = $this->language->get('text_disabled');
$data['text_content_top'] = $this->language->get('text_content_top');
$data['text_content_bottom'] = $this->language->get('text_content_bottom');
$data['text_column_left'] = $this->language->get('text_column_left');
$data['text_column_right'] = $this->language->get('text_column_right');
$data['entry_code'] = $this->language->get('entry_code');
$data['entry_layout'] = $this->language->get('entry_layout');
$data['entry_position'] = $this->language->get('entry_position');
$data['entry_status'] = $this->language->get('entry_status');
$data['entry_sort_order'] = $this->language->get('entry_sort_order');
$data['button_save'] = $this->language->get('button_save');
$data['button_cancel'] = $this->language->get('button_cancel');
$data['button_add_module'] = $this->language->get('button_add_module');
$data['button_remove'] = $this->language->get('button_remove');
// This Block returns the warning if any
if (isset($this->error['warning'])) {
$data['error_warning'] = $this->error['warning'];
} else {
$data['error_warning'] = '';
}
// This Block returns the error code if any
if (isset($this->error['code'])) {
$data['error_code'] = $this->error['code'];
} else {
$data['error_code'] = '';
}
// Making of Breadcrumbs to be displayed on site
$data['breadcrumbs'] = array();
$data['breadcrumbs'][] = array(
'text' => $this->language->get('text_home'),
'href' => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
'separator' => false
);
$data['breadcrumbs'][] = array(
'text' => $this->language->get('text_module'),
'href' => $this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL'),
'separator' => ' :: '
);
$data['breadcrumbs'][] = array(
'text' => $this->language->get('heading_title'),
'href' => $this->url->link('module/mysite', 'token=' . $this->session->data['token'], 'SSL'),
'separator' => ' :: '
);
$data['action'] = $this->url->link('module/mysite', 'token=' . $this->session->data['token'], 'SSL');
$data['cancel'] = $this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL');
// This block parses the status (enabled / disabled)
if (isset($this->request->post['mysite_status'])) {
$data['mysite_status'] = $this->request->post['mysite_status'];
} else {
$data['mysite_status'] = $this->config->get('mysite_status');
}
$data['header'] = $this->load->controller('common/header');
$data['column_left'] = $this->load->controller('common/column_left');
$data['footer'] = $this->load->controller('common/footer');
$this->response->setOutput($this->load->view('module/mysite.tpl', $data));
}
protected function validate()
{
// Block to check the user permission to manipulate the module
if (!$this->user->hasPermission('modify', 'module/mysite')) {
$this->error['warning'] = $this->language->get('error_permission');
}
// Block returns true if no error is found, else false if any error detected
if (!$this->error) {
return true;
} else {
return false;
}
}
/* Event Handlers */
public function on_order_history_add($order_id)
{
// Load order data
$this->load->model('checkout/order');
$orderData = $this->model_checkout_order->getOrder($order_id);
// Debug
$this->logger->write(print_r($orderData, true));
// TODO: call third-party api & create hosting package
// TODO: send success/error email notification to customer
}
}
管理\语言\英语\模块\ mysite.php
<?php
// Heading
$_['heading_title'] = 'My Site';
// Text
$_['text_module'] = 'Modules';
$_['text_edit'] = 'Edit My Site Module';
$_['text_success'] = 'Success: You have modified module My Site!';
// Entry
$_['entry_status'] = 'Status:';
// Error
$_['error_permission'] = 'Warning: You do not have permission to modify module My Site!';
$_['error_code'] = 'Code Required';
管理\视图\模板\模块\ mysite.tpl
<?php echo $header; ?><?php echo $column_left; ?>
<div id="content">
<div class="page-header">
<div class="container-fluid">
<div class="pull-right">
<button type="submit" form="form-helloworld" data-toggle="tooltip" title="<?php echo $button_save; ?>" class="btn btn-primary"><i class="fa fa-save"></i></button>
<a href="<?php echo $cancel; ?>" data-toggle="tooltip" title="<?php echo $button_cancel; ?>" class="btn btn-default"><i class="fa fa-reply"></i></a></div>
<h1><?php echo $heading_title; ?></h1>
<ul class="breadcrumb">
<?php foreach ($breadcrumbs as $breadcrumb) { ?>
<li><a href="<?php echo $breadcrumb['href']; ?>"><?php echo $breadcrumb['text']; ?></a></li>
<?php } ?>
</ul>
</div>
</div>
<div class="container-fluid">
<?php if ($error_warning) { ?>
<div class="alert alert-danger"><i class="fa fa-exclamation-circle"></i> <?php echo $error_warning; ?>
<button type="button" class="close" data-dismiss="alert">×</button>
</div>
<?php } ?>
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title"><i class="fa fa-pencil"></i> <?php echo $text_edit; ?></h3>
</div>
<div class="panel-body">
<form action="<?php echo $action; ?>" method="post" enctype="multipart/form-data" id="form-helloworld" class="form-horizontal">
<div class="form-group">
<label class="col-sm-2 control-label" for="input-status"><?php echo $entry_status; ?></label>
<div class="col-sm-10">
<select name="mysite_status" id="input-status" class="form-control">
<?php if ($mysite_status) { ?>
<option value="1" selected="selected"><?php echo $text_enabled; ?></option>
<option value="0"><?php echo $text_disabled; ?></option>
<?php } else { ?>
<option value="1"><?php echo $text_enabled; ?></option>
<option value="0" selected="selected"><?php echo $text_disabled; ?></option>
<?php } ?>
</select>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
<?php echo $footer; ?>
当我安装模块时(我在WAMP中开发),我收到以下错误:
致命错误:在第10行的H:\ wamp \ www \ mysite.local \ system \ engine \ controller.php中的非对象上调用成员函数get()
任何想法这个错误是什么?当我刷新页面时,它确实说安装了模块,当我尝试卸载它时,我得到了同样的错误。
答案 0 :(得分:2)
原始构造函数正在设置$this->registry
对象。您正在覆盖构造函数(system/engine/controller.php:2
)。所以使用
public function __construct($registry)
{
parent::__construct( $registry );
$this->error = array();
$this->logger = new Log('MySite.log');
}