Skip to main contentSkip to footer

Front end Controller file to make hello world - OpenCart Module Development

Rupak Nepali
Share:
Front end Controller file to make hello world - OpenCart Module Development

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 the 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 is 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 a 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 the view section, catalog\view\theme\default\template\extension\module\helloworld.tpl, you can write the following code and show the output:

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

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

controller-helloworld-module-opencart

Comments

Join the conversation and share your thoughts

Leave a Comment

Your comment will be reviewed before publishing.

Be the first to comment on this post!

Innovation

Let's Make Something Amazing Together

We always provide our best creative ideas at the highest level. Tell us about your project and we will make it work.

InnovateBringing innovative solutions to complex problems
AutomateStreamlining processes through automation
DominateLeading the market with exceptional results
Build Smarter and Launch FasterEfficient development for rapid deployment