OpenCart identifies existing modules automatically, simply by reading the admin/controller/extension/module folder. Any modules existing in this folder will automatically be shown on the Modules listing page, and on the User Permissions page. Let’s start to create our hello world module by creating the file at admin/controller/extension/module and named it helloworld.php. Then follow following steps:

Create a class named ControllerExtensionModuleHelloWorld

Every module name should start with ControllerExtensionModule in OpenCart.
Use camel case style, avoid using special characters, url path will be “extension/module/modulename” in our helloworld case it will be “extension/module/helloworld”.

Extends it with base class Controller

class ControllerExtensionModuleHelloWorld extends Controller {}

Create public function named index

public function index(){}

This will load every time when we open the helloworld module
Opencart provides a Response class a simple PHP representation of an HTTP response message. This allows your applications to use a n object-oriented interface to construct the response that needs to be returned to the client.

$this->response->setOutput($this->load->view('extension/module/helloworld', $data));

This loads the view helloworld.tpl from admin\view\template\extension\module\helloworld.tpl pass data from controller to view.

How to pass header, footer and column left section for helloworld module?

$data['header'] = $this->load->controller('common/header');
$data['column_left'] = $this->load->controller('common/column_left');
$data['footer'] = $this->load->controller('common/footer');

How to pass language data to view section?

The controller file is the place where we can load the language files to convert text into variables to be utilized in the template file. We can load the language file as below:

$this->load->language('extension/module/helloworld');

It means that the language file named helloworld.php is at admin/language/ACTIVE_LANGUAGE_FOLDER/extension/module

Then setup into data variable as:

$data['view_variable'] = $this->language->get('language_variable');

Now if you see the echo the heading_title at view file admin\view\template\extension\module\helloworld.tpl then you will see the output as following images.

Admin Controller file to make hello world module – OpenCart Module Development

  • In the view section:
    //adding breadcrumbs
    $data['breadcrumbs'] = array();
    $data['breadcrumbs'][] = array(
    'text' => $this->language->get('text_home'),
    'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], true)
    );
    $data['breadcrumbs'][] = array(
    'text' => $this->language->get('text_extension'),
    'href' => $this->url->link('extension/extension', 'token=' . $this->session->data['token'] . '&type=module', true)
    );
    $data['breadcrumbs'][] = array(
    'text' => $this->language->get('heading_title'),
    'href' => $this->url->link('extension/module/helloworld', 'token=' . $this->session->data['token'], true)
    );
  • In the view section:
    <ul class="breadcrumb">
    <?php foreach ($breadcrumbs as $breadcrumb) { ?>
    <li><a href="<?php echo $breadcrumb['href']; ?>"><?php echo $breadcrumb['text']; ?></a></li>
    <?php } ?>
    </ul>

How to add save and cancel button?

  • In the controller:
    if (!isset($this->request->get['module_id'])) {
    $data['action'] = $this->url->link('extension/module/helloworld', 'token=' . $this->session->data['token'], true);
    } else {
    $data['action'] = $this->url->link('extension/module/helloworld', 'token=' . $this->session->data['token'] . '&module_id=' . $this->request->get['module_id'], true);
    }
    
    $data['cancel'] = $this->url->link('extension/extension', 'token=' . $this->session->data['token'] . '&type=module', true);
  • In the view section:
    <div class="pull-right">
        <button type="submit" form="form-html" 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>
    

Setting up data variable value to pass to view section:

if (isset($this->request->post['helloworld_text'])) {   
 $data['helloworld_text'] = $this->request->post['helloworld_text'];
} elseif (!empty($module_info)) {    
$data['helloworld_text'] = $module_info['helloworld_text'];
} else {    
$data['helloworld_text'] = '';
}

Get Submitted value of form in controller:

  • Check whether form is submitted or not
    $this->request->server['REQUEST_METHOD'] == 'POST'
  • Get the submitted value of form in controller:
    $this->request->post['name']
  • Get the url value:
    $this->request->get['module_id']

Get the modules values:

 if (isset($this->request->get['module_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
            $module_info = $this->model_extension_module->getModule($this->request->get['module_id']);
 }

Validating the input type:

protected function validate() {
        if (!$this->user->hasPermission('modify', 'extension/module/helloworld')) {
            $this->error['warning'] = $this->language->get('error_permission');
        }
        if ((utf8_strlen($this->request->post['name']) < 3) || (utf8_strlen($this->request->post['name']) > 64)) {
            $this->error['name'] = $this->language->get('error_name');
        }
        return !$this->error;
    }

Permission Management

  • In the controller section:
    In the index function
if (isset($this->error['warning'])) {
    $data['error_warning'] = $this->error['warning'];
} else {
    $data['error_warning'] = '';
}

In the validate function

if (!$this->user->hasPermission('modify', 'extension/module/helloworld')) {
    $this->error['warning'] = $this->language->get('error_permission');
}

In the view section:

<?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">&times;</button>
</div>

 

Full code is below:

<?php

/**
 * Created by PhpStorm.
 * User: rnepali
 * Date: 10/1/2016
 * Time: 6:20 PM
 */class ControllerExtensionModuleHelloWorld extends Controller {

    public function index(){
        //load the language file
        $this->load->language('extension/module/helloworld');
        //pass language file data 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['entry_name'] = $this->language->get('entry_name');
        $data['entry_title'] = $this->language->get('entry_title');
        $data['entry_status'] = $this->language->get('entry_status');
        $data['button_save'] = $this->language->get('button_save');
        $data['button_cancel'] = $this->language->get('button_cancel');

        if (isset($this->error['warning'])) {
            $data['error_warning'] = $this->error['warning'];
        } else {
            $data['error_warning'] = '';
        }

if (isset($this->error['error_name'])) {
            $data['error_name'] = $this->error['error_name'];
        } else {
            $data['error_name'] = '';
        }

        $this->load->model('extension/module');
        if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {

            if (!isset($this->request->get['module_id'])) {
                $this->model_extension_module->addModule('helloworld', $this->request->post);
            } else {
                $this->model_extension_module->editModule($this->request->get['module_id'], $this->request->post);
            }

            $this->session->data['success'] = $this->language->get('text_success');

            $this->response->redirect($this->url->link('extension/extension', 'token=' . $this->session->data['token'] . '&type=module', true));
        }


        //adding breadcrumbs
        $data['breadcrumbs'] = array();
        $data['breadcrumbs'][] = array(
            'text' => $this->language->get('text_home'),
            'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], true)
        );
        $data['breadcrumbs'][] = array(
            'text' => $this->language->get('text_extension'),
            'href' => $this->url->link('extension/extension', 'token=' . $this->session->data['token'] . '&type=module', true)
        );
        $data['breadcrumbs'][] = array(
            'text' => $this->language->get('heading_title'),
            'href' => $this->url->link('extension/module/helloworld', 'token=' . $this->session->data['token'], true)
        );
        
        if (isset($this->request->get['module_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
            $module_info = $this->model_extension_module->getModule($this->request->get['module_id']);
        }

        if (isset($this->request->post['name'])) {
            $data['name'] = $this->request->post['name'];
        } elseif (!empty($module_info)) {
            $data['name'] = $module_info['name'];
        } else {
            $data['name'] = '';
        }

        if (isset($this->request->post['status'])) {
            $data['status'] = $this->request->post['status'];
        } elseif (!empty($module_info)) {
            $data['status'] = $module_info['status'];
        } else {
            $data['status'] = '';
        }
        
        if (!isset($this->request->get['module_id'])) {
            $data['action'] = $this->url->link('extension/module/helloworld', 'token=' . $this->session->data['token'], true);
        } else {
            $data['action'] = $this->url->link('extension/module/helloworld', 'token=' . $this->session->data['token'] . '&module_id=' . $this->request->get['module_id'], true);
        }

        $data['cancel'] = $this->url->link('extension/extension', 'token=' . $this->session->data['token'] . '&type=module', true);

        $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('extension/module/helloworld', $data));
    }

    protected function validate() {
        if (!$this->user->hasPermission('modify', 'extension/module/helloworld')) {
            $this->error['warning'] = $this->language->get('error_permission');
        }
        if ((utf8_strlen($this->request->post['name']) < 3) || (utf8_strlen($this->request->post['name']) > 64)) {
            $this->error['name'] = $this->language->get('error_name');
        }
        return !$this->error;
    }

}

thanking_you

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here