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:
Hello Rupak, first thanks for this tutorials….
I have a question.. in my frontend: mydomain/index.php?route=extension/module/helloworld
dont show the page. I see:
The page you requested cannot be found!
And it is enabled in the admin, and just in case, I deleted the cache directory and it was recreated on the server.
Is wrong the URL ?
Thanks again!