front_end_controller_file_to_make_hello_world

OpenCart identifies existing modules automatically, simply by reading the catalog/controller/extension/module folder. Any modules existing in this folder will automatically be loaded as per the active modules for the pages on the front end.

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 frontend also.
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”.

Code written for Front end Controller file to make hello world – OpenCart Module Development

<?php
class ControllerExtensionModuleHelloWorld extends Controller {
    public function index($setting) {
        $data="";
        if (isset($setting['name'])) {
            $data['name'] = $setting['name'];
        }
        return $this->load->view('extension/module/helloworld', $data);
    }
}

In the above code for helloworld module which will show text at the front end,

  • Class name is ControllerExtensionModuleHelloWorld
  • This class extends the base Controller class
  • Have public index function declaring $setting as arguments
  • This $setting holds all the values available for the module. For example when we do print_r for the $setting we find following:
    Array
    (
        [name] => This is hello world module text
        [status] => 1
    )
  • So we can assign to $data array to pass it to view section like:
    if (isset($setting['name'])) {
          $data['name'] = $setting['name'];
    }
  • Then return the loaded view as:
    return $this->load->view('extension/module/helloworld', $data);

    Now in view section, catalog\view\theme\default\template\extension\module\helloworld.tpl, you can write following code and show the output:

    <div>
        <h2><?php echo $name; ?></h2>
    </div>

    Like this way you can show hello world text at front end:

    controller-helloworld-module-opencart

LEAVE A REPLY

Please enter your comment!
Please enter your name here