Documentation - Modules

Modules

Overview

OpenCart utilizes a Model-View-Controller (MVC) structure. MVC separates code by concern, allowing developers to maintain and extend small segments of code specific to a given function verses a plethora of nested, interdependent files. It is highly recommended that you familiarize yourself with the form and function of MVC before proceeding to modify and extend OpenCart. For more information regarding MVC, please refer to Wikipedia.

Where to Start

We start with the files that are needed in order to create your module.

 

 

For the admin section:

 

admin/controller/module/module_name.php
admin/language/english/module/module_name.php
admin/view/template/module/module_name.tpl

 

For the front end(if required by your module):

 

catalog/controller/module/module_name.php
catalog/language/english/module/module_name.php
catalog/view/theme/default/template/module/module_name.tpl


 

The above are the main files that would be used by your module, so in total 6 files. Next, we should touch on each one and explain in simple terms what they are used for.

 

The Admin Controller

This is the controller file for the admin section. For those of you who are not familiar with the use of this file, I will try to explain it clearly here. This file is the building block or your module (admin and catalog). This file does not produce anything to the end user, it 'constructs' the data that will be passed to the view file. Essentially, the controller is a PHP class and nothing more.

So what must you do to create this controller file? First you need to start the class:

 

 

<?php class ControllerModuleName extends Controller {

private $error = array();
public function index() {
 
}

} ?>


 

That is the basis of your Controller. Now you have the use of the built in error control and the start of your modules controller. Of course this will not work without it having us enter the rest of the code. Not to mention, what language file should be used, how is this module going to be displayed...still a few more steps!

So let's get back to the basics of your admin controller.

 

 

<?php class ControllerModuleName extends Controller {

private $error = array();
public function index() {
$this->load->language('module/modulename'); // THIS IS LOCATED UNDER YOUR ADMIN DIRECTORY
$this->document->title = $this->language->get('heading_title');
}

} ?>


 

Now what we have done here is said that there is a language file located in admin/language/module/modulename.php. Notice that with the PHP Framework you do not need to include the .php. Next we are grabbing the 'heading_title' from the language file so that it stores the title in document->title(which will be used in the view of your module).

Moving on...if you want to utilize any of the built in OpenCart features make sure to include the following in your code (which is admin/controller/setting/setting.php):

 

 

<?php class ControllerModuleName extends Controller {

private $error = array();
public function index() {
$this->load->language('module/modulename'); // THIS IS LOCATED UNDER YOUR ADMIN DIRECTORY
$this->document->title = $this->language->get('heading_title');
$this->load->model('setting/setting');
}

} ?>


 

Ce site est maintenu par l'agence ONLYTECH spécialisée en création de sites e-commerce
Découvrez de nouveaux tutoriaux et ressources pour le développement Web sur www.ressources-du-web.com
L'actualité High-tech et Web sur www.passion-hitech.info