OpenCart MVC is really MVC-L: each page or module is built from four parts. The controllerCode that receives a request and prepares data. More in the glossary → (Controller) receives the request and manages the work. The model (Model) talks to the database. The Twig template (View) renders the HTML. The language fileA PHP file with interface texts, separate for each language. More in the glossary → (Language) stores the texts. All four parts are linked through the parameter route and identical relative paths, so once you know the route, you immediately know where each file is.
Examples are for OpenCart 4.1; in 4.0.x, model triggers and event handling differ.
What is MVC-L in OpenCart and why is there an "L"?
Classic MVC separates logic, data, and presentation. OpenCart adds a fourth layer: language files. The reason is practical: a store is almost always multilingual. In Ukraine, that means at least Ukrainian, and often English as well. Interface texts are moved into separate PHP arrays so that templates and controllers contain no hard-coded strings.
The key idea: all parts of one page share the same relative path. For a product pageThe page for a single product: photos, price, options, description, reviews. More in the glossary →, this is product/product. The controller, language file, and template are located along it. The model usually has its own path based on the entity, for example catalog/product. This convention is the main "glue" of the architecture: there are no route registries or XML configs, only file paths.
| Layer | OpenCart 3.0 | OpenCart 4.1 |
|---|---|---|
| Controller | catalog/, class ControllerProductProduct | catalog/, class Opencart\Catalog\Controller\Product\Product |
| Model | catalog/, class ModelCatalogProduct | catalog/, class Opencart\Catalog\Model\Catalog\Product |
| Template | catalog/ | catalog/ |
| Language | catalog/ | catalog/ |
The admin panel is structured the same way, only in the admin/ directory. The storefrontWhat the customer sees: the home page, categories, product page, and cart. More in the glossary → and the admin are two separate applications on a shared core; for more detail, see the article about admin and catalog in OpenCart. The overview of the file structure of OpenCart 3.0 and 4.1 gives the general map of directories.
How does a request reach the controller via the route?
Every HTTP request passes through index.php. Then the core initializes the registry, config, database, session, and language, and then runs startup actions. Among them is SEO URL, which converts a "pretty" address into route. The full path from the first line to the method call is covered in the article about framework.php and the request path. What matters here is how route is converted into a class and method.
flowchart TD
A["Request: index.php?route=..."] --> B["framework.php: registry, config, DB, session"]
B --> C["Startup: SEO URL, language, currency"]
C --> D["Router: route → controller file and method"]
D --> E["Controller: load->language, load->model"]
E --> F["Model: $this->db->query()"]
F --> E
E --> G["load->view: Twig + $data"]
G --> H["Response: setOutput and HTML output"]
OpenCart 3.0: the route checkout/cart/add is parsed as follows. The core looks for the file checkout/cart.php, and if no such file exists, it treats the last segment as the method name (add). Without a method, index() is called.
OpenCart 4.1: the method is separated by a dot (checkout/cart.add); the old separator | is automatically replaced with a dot. The file path and the method are no longer confused, and the class is found by its namespace.
Routing nuances (prohibiting calls to "private" methods, 404 handling, version differences) are collected in a separate article in the section about route=. To understand MVC-L, it is enough to remember that a route is the path to the controller file plus, if needed, a method.
What does the controller do and how does it pull in the other layers?
The controller is the conductor. It knows almost nothing about SQL or HTML. It loads the language, calls the model, assembles the $data array, and passes it to the template. All services ($this->db, $this->config, $this->session, $this->load) are available through magic properties that are read from the OpenCart object registry.
Here is a minimal storefront module that outputs a product list. First, the version for 3.0:
Example
<?php
class ControllerExtensionModuleHello extends Controller {
public function index($setting) {
$this->load->language('extension/module/hello');
$this->load->model('extension/module/hello');
$data['heading_title'] = $this->language->get('heading_title');
$data['items'] = $this->model_extension_module_hello->getItems((int)$setting['limit']);
return $this->load->view('extension/module/hello', $data);
}
}
The same controller for 4.1, if the extension is called mymodule:
Example
<?php
namespace Opencart\Catalog\Controller\Extension\Mymodule\Module;
class Hello extends \Opencart\System\Engine\Controller {
public function index(array $setting): string {
$this->load->language('extension/mymodule/module/hello');
$this->load->model('extension/mymodule/module/hello');
$data['heading_title'] = $this->language->get('heading_title');
$data['items'] = $this->model_extension_mymodule_module_hello->getItems((int)$setting['limit']);
return $this->load->view('extension/mymodule/module/hello', $data);
}
}
Note: a module in a layout is called as a child controller, so it returns a string via return rather than calling $this. The "main" controller of a page does the opposite. It collects the parts via $this and hands the result to the Response object. If you confuse these roles, you will get either an empty block or a page that is output twice.
How is the model connected to the controller and the database?
The model is the only place where SQL should live. The call $this includes the file model/. Then the object is registered in the registry under a key obtained by replacing slashes with underscores: model_catalog_product. Hence the access $this. The loading mechanics are described in the article about the Loader and the load->model, view, language methods.
Example (OpenCart 4.1; in 3.0 it is the class ModelExtensionModuleHello extends Model without a namespace)
<?php
namespace Opencart\Catalog\Model\Extension\Mymodule\Module;
class Hello extends \Opencart\System\Engine\Model {
public function getItems(int $limit): array {
$query = $this->db->query("SELECT p.`product_id`, pd.`name` FROM `" . DB_PREFIX . "product` p LEFT JOIN `" . DB_PREFIX . "product_description` pd ON (p.`product_id` = pd.`product_id`) WHERE p.`status` = '1' AND pd.`language_id` = '" . (int)$this->config->get('config_language_id') . "' LIMIT " . (int)$limit);
return $query->rows;
}
}
A few things worth knowing:
- The model is called through a proxy. The Loader wraps the model so that events can fire before and after each method: in 3.0 this is
catalog/(slash before the method), in 4.1 it ismodel/ catalog/ product/ getProduct/ before catalog/(dot before the method). Because of this, third-party modules can substitute data without touching the model file. A trigger with the wrong separator simply won't fire, and no error will be shown.model/ catalog/ product.getProduct/ before - Storefront and admin models are different.
admin/can add and edit products, while the storefront version only reads. The admin model cannot be included from a storefront controller, and that is correct.model/ catalog/ product.php - Escaping is your responsibility.
$thisfor strings, casting with->db ->escape() (int)for numbers. The mysqli driver has no public API for prepared statements (in 4.1 the pdo driver usesprepare()only internally). Therefore, in both branches, cast numbers to the proper type, escape strings, and validate column names against a whitelist.
The article in the section on models and $this->db explains queries, DB_PREFIX, $query->row, and $query->rows in more depth.
How do the Twig template and the language file receive data?
The template receives only what the controller placed in $data. Each array key becomes a Twig variable: $data['items'] becomes {{ items }}. The template should contain as little logic as possible: loops, conditions, and output.
Example
<div class="hello-module">
<h3>{{ heading_title }}</h3>
{% if items %}
<ul>
{% for item in items %}
<li>{{ item.name }}</li>
{% endfor %}
</ul>
{% else %}
<p>{{ text_empty }}</p>
{% endif %}
</div>
The language file is a regular PHP array $_:
Example: catalog/ (3.0)
<?php
$_['heading_title'] = 'Нові надходження';
$_['text_empty'] = 'Поки що товарів немає.';
Here lies a nuance that often causes confusion. The template contains {{ text_empty }}, although the controller did not pass it explicitly. The core has an event on view/*/before that mixes into the template data all strings loaded via load->language(), if such a key is not yet present in $data. That is why many language variables in core templates have no explicit assignment. You can rely on this. But if a value from $data and a language string share the same key, $data wins, and this is worth keeping in mind.
OpenCart 3.0: storefront templates are located in the theme folder (catalog/). If your theme does not have a file, it is taken from default.
OpenCart 4.1: base templates are located in catalog/, and themes are supplied as extensionsAdditional functionality: a module, a payment or shipping method, a feed, or a theme. They are managed in "Extensions → Extensions." More in the glossary → and replace templates through events. Extension templates are located in extension/.
Compiled Twig templates are cached in the storage directory. If changes in .twig are not visible, start with it. Details are in the article about the storage directory, cache, and modifications. You can translate strings without editing files via "Design → Language Editor". Overrides are stored in the database and take priority over the file.

How do you organize the files of your own module so that everything connects?
Most errors like "Could not load model" or "Error: Could not load template" come from a mismatch between the path in the load->... call and the actual file path. Stick to a simple scheme.
OpenCart 3.0: module in shared folders
- Admin controller:
admin/, classcontroller/ extension/ module/ hello.php ControllerExtensionModuleHello. - Admin language:
admin/(andlanguage/ en-gb/ extension/ module/ hello.php uk-ua, if the language is installed). - Admin template:
admin/.view/ template/ extension/ module/ hello.twig - Storefront:
catalog/,controller/ extension/ module/ hello.php catalog/,model/ extension/ module/ hello.php catalog/,language/ .../ extension/ module/ hello.php catalog/.view/ theme/ default/ template/ extension/ module/ hello.twig
OpenCart 4.1: module as a separate extension
- Everything is in one folder:
extension/mymodule/. Inside are the subfoldersadmin/andcatalog/with the usual structurecontroller,model,language,view/template. - The namespace is built from the path:
Opencart\Admin\Controller\Extension\Mymodule\Moduleforextension/.mymodule/ admin/ controller/ module/ hello.php - In core calls, the extension name follows
extension/:load.->view('extension/ mymodule/ module/ hello') - The extension is packed into an
.ocmod.ziparchive and installed via "Extensions → Installer".
After installation, the module appears in "Extensions → Extensions" if "Modules" is selected in the type filter. The "Install" button calls the install() method of your admin controller. This is usually where tables are created and events are registered. The access and modify permissions for the current user's group are granted automatically by the core in both branches. Other groups are granted them as needed via the addPermission() method of the user/user_group model. When uninstalling, 3.0 removes the module's permissions from all groups, while 4.1 does not remove them.

Typical pitfalls when organizing files:
- The class name does not match the path. In 3.0,
ControllerExtensionModuleHelloWorldcorresponds to the filehello_world.php, because underscores from the path are removed. - The language file exists only in
en-gb, but the store runs in Ukrainian. If there is no language file inuk-ua, in 3.0 English strings fromen-gbwill be shown. In 4.1,en-gbis not loaded as a fallback, so without a file inuk-ua, the keys themselves will be displayed instead of the text (heading_title, etc.). To have the interface in Ukrainian, add the translation touk-ua. - In 4.1, the autoloader will not find a class if the namespace does not match the file path. It is advisable to specify return types for compatibility with the core, but they do not affect class loading.
- In the admin panel, links without
user_tokenredirect to login. Build them via$thiswith the session token. There is a separate article on protecting the panel in general: OpenCart admin security.->url ->link()
How does MVC-L in OpenCart 3.0 differ from 4.1 in practice?
The idea is the same, but the "mechanics" in 4.1 have become stricter and closer to modern PHP. Here is a short list of changes that most often break migrated modules:
| Aspect | OpenCart 3.0 | OpenCart 4.1 |
|---|---|---|
| Class names | Concatenated from the path: ControllerCatalogProduct | Namespaces: Opencart\Admin\Controller\Catalog\Product |
| Method in route | catalog/ | catalog/ (the old | is automatically replaced with a dot) |
| Saving forms in admin | POST to the same page, validation and redirect | AJAX to the .save method, response in JSON |
| Module location | Scattered across admin/ and catalog/ | One extension/ folder |
| Themes | Folder in catalog/view/theme/ | Extension of type "theme", replaced via events |
Because of these differences, a module from 3.0 will not work in 4.1 by simply copying it. You need to rename the classes, rewrite the routes, move the files into the extension folder, and rework the admin form for AJAX. The storefront logic (SQL in models, Twig templates, language arrays) transfers almost unchanged, and this is the best argument for keeping it clean and separated. If such a migration is needed on a live store without risk, you can hand it over to OpenCart fixes and bug-fixing services. But with this article and the core documentation, it is entirely feasible to do it on your own.
Summary
- OpenCart MVC is MVC-L: the controller manages, the model works with the DB, the Twig template outputs HTML, and the language file stores the texts.
- The parts are connected by a path convention, not configs. The route determines the controller file, which through
$this->loadpulls in the language, model, and template. - Models are registered as
model_шлях_через_підкресленняand called through a proxy, so events can be attached to them. - Child controllers return a string; main controllers output through the Response.
- In 4.1, there are namespaces, a dot as the method separator in routes, modules in a separate
extension/folder, and AJAX form saving.
Next, go through the articles in the section one by one: routing, controllers, models, Twig, and language files. They are all collected on the section page MVC-L: controllers, models, templates. The best practice is to write a small module like the example above for your version right away and make sure all four layers find each other.