MVC (Model View Controller)

In Opencart, Model, View and Controller are the three layered which are used for passing the data to each other layered in a well defined manner.

1.Controller: In Opencart, Controller play a role of mediator which manage the whole control of program. while any customer hit a URL through browser, then the controller file will be called. Inside the controller we can load the model and call methods of that model file to get the related data. After getting response from model file controller send the data to the view file. We can also include the JS and CSS files in Opencart’s controller.

2.Model: Model file used to fetch the data from database by using SQL queries and return same data to controller. Model file mainly used to perform some operation with database through SQL Commands like DDL (Data Definition Language i.e. Create, Alter, Drop, Truncate, etc.) and DML (Data Manipulation Language i.e. Select, Insert, Update, Delete,Merge, etc.).


3.View: View file receives the resultant data from controller file in php, json, etc format and view that data to the browsers. You can write the HTML, JS/JQuery, Css and PHP code in view file in opencart. We use the .tpl Extension for the view file.


4. Language: We also use a file called language file for sting mapping with the array index. Controller loads this language file by fetching the all the index with their string conversion and render those array index to the .tpl file.

Route Flow of Opencart

  • admin/index.php, index.php
  • Script,Style
    • Document :: addScript( )
    • Document :: addStyle( )
  • Controller Processing
    • Loader :: controller($route)
    • Action :: execute($this->registry, array(&$data))
  • Model Processing
    • Loder :: model($route)
    • include file- modification(file_name)
  • Rendering View file
    • Template :: render($route.’.tpl’)
  • Config Processing
    • Config :: load($route)
  • Event processing
    • Event :: register($trigger, $action)
    • Event :: trigger($event)


A) Initial Phase in Opencart

In start, the index.php file called when you hit your opencart store site url in browser and inside the index.php file, its include the statup.php in which you will find the code for validate the php version and set the default time zone.

In startup.php file we have a method named as modification($filename) which returns the full matched path of modification files only if your are using ocmod.xml file:

You will also find the start(‘catalog’) inside the system/startup.php file which will load the framework.php file in which all the used classes’s object are created and set to the registry. By which you can access all the system classes into the controller.

B) Controller Architecture

Controller Class declaration is same for both admin and catalog side. If you will open any controller from  admin or catalog end, you will find class name like as:


You should be always write Controller keyword first than your file path in Camel Case without any whitespace. In any controller file, index method calls first while hit the controller route through URL.

  B.1) Nested Controller

You can also include other controller within main controller by using below code:



B.2) Load Script and Style Sheet

There are two way to load the style and script file in opencart, Either you can load these file through controller or define on view file(.tpl). To load on controller you have to use below command:


B.3) Load the Model File

You can load the model on controller and can call model function by using the following command:


B.4) Render the Template(View) File

To send/view the data from controller to view (.tpl) file you have to use below code:

Thanks –