No results found

Try a different search query

Popular searches:

Add to Cart

Cart

You have no purchases yet

Browse Marketplace
Original Subtotal:
Product discounts:
Quantity Discount:
Total:
View Cart
Architecture and files

OpenCart admin and catalog: two applications on one core

Why the OpenCart admin and storefront have separate index.php, configs, models and templates, what they share, and how to account for this in modules for 3.0 and 4.1.

11 min read OC 3.0 OC 4.1

In OpenCart, admin and catalog are two separate MVC applications. Each has its own entry point, config, controllers, models, language files, and templates. The only things they share are the system/ core and the database. If you keep the model "opencart admin catalog = two applications, one core" in mind, it becomes clear why a module consists of two halves, why the admin product model is unsuitable for the storefrontWhat the customer sees: the home page, categories, product page, and cart. More in the glossary →, and why an event without the correct prefix simply doesn't fire.

Examples are for OpenCart 4.1; in 4.0.x, model triggers and event handling differ.

Why is OpenCart split into admin and catalog?

The admin panel and the storefront solve different tasks, so they have different requirements. The storefront serves anonymous visitors and customers. It needs SEO URLs, the cart, currency, taxes, the customer session, and fast catalog reads. The admin panel needs manager authentication, an access-rights check on every route, a CSRF token in the URL, and full CRUD operations on data. If everything lived in one application, every storefront request would carry the admin's extra checks, and every admin request would carry the cart logic.

That is why OpenCart puts them in separate directories, and each request is handled from start to finish by only one of the applications:

  • catalog: the storefront. The site root, index.php and config.php in the root, code in catalog/;
  • admin: the control panel. admin/index.php and admin/config.php, code in admin/;
  • system: the shared core. The engine (ControllerCode that receives a request and prepares data. More in the glossary →, Model, Loader, Registry, Event), libraries (DB, Request, Response, Session, Cache, Url, Template), helpers, and base configs.

A detailed directory map for both versions is described in the article about the OpenCart file structure. Here we'll look at what this separation means for a developer.

In practice, there is no "magic" in cross-application code calls. An admin controller cannot directly call a storefront controller via $this->load->controller(), and vice versa. The Loader always looks for files relative to the current application, that is, relative to DIR_APPLICATION.

How does one framework run two different applications?

Both entry points work the same way: they include their own config.php with path constants, then system/startup.php, and then hand control to the shared framework, telling it the application name: catalog or admin. The framework loads the base system/config/default.php, and on top of it the config of the specific application: system/config/catalog.php or system/config/admin.php. It is this second file that turns the same core into either the storefront or the admin panel. The request path is broken down step by step in the article framework.php: from index.php to the controller.

flowchart TD
  A["/index.php"] --> C["config.php storefront"]
  B["/admin/index.php"] --> D["admin/config.php"]
  C --> E["system/startup.php"]
  D --> E
  E --> F["system/framework.php"]
  F --> G["system/config/default.php"]
  G --> H{"Which application?"}
  H -->|"catalog"| I["system/config/catalog.php"]
  H -->|"admin"| J["system/config/admin.php"]
  I --> K["storefront startup actions: session, settings, SEO URL"]
  J --> L["admin startup actions: login, permissions, token"]
  K --> M["Controller catalog/controller/..."]
  L --> N["Controller admin/controller/..."]

The application config defines several key things. The first is the default route: for the storefront it is the home page (common/home), for the admin panel it is the dashboard (common/dashboard). The second is the list of pre-actions, that is, startup controllers that run before the main one. The third is the autoloading of libraries and language files. The fourth is whether the session starts automatically.

The constants in config.php are also different. In the storefront, DIR_APPLICATION points to catalog/, while in the admin panel it points to admin/. The admin config has additional constants HTTP_CATALOG and DIR_CATALOG so that the admin panel can build links to the storefront and know where its files are located. How file configs are combined with settings from the setting table is covered in the article about config.php and system/config.

How do admin and catalog differ in practice?

The differences are easiest to see in a table. They are the same in essence in both versions, although the specific names of startup controllers changed in 4.1.

Aspectcatalog (storefront)admin (admin panel)
Entry point/index.php/admin/index.php
File config/config.php/admin/config.php
Application configsystem/config/catalog.phpsystem/config/admin.php
Default routehome pagedashboard (or login if not authenticated)
User objects in Registrycustomer, cart, currency, taxuser
Route protectioncustomer login check in individual controllersstartup checks for login, user_token, and group permissions
Templates3.0: catalog/view/theme/<тема>/template/; 4.1: catalog/view/template/ plus theme extensionsadmin/view/template/
Language filescatalog/language/<код>/admin/language/<код>/
SEO URLyes, via a startup controllerno, only ?route=

Objects such as $this->customer or $this->user appear in the Registry only in their "own" application. The classes are shared in system/library/cart/, but they are registered by the startup controllers of the specific application. This leads to a common mistake: accessing $this->customer->isLogged() from an admin controller results in a fatal error or an empty result, because that object simply doesn't exist in the admin panel.

Another difference is the token. In the admin panel, every link contains user_token from the session, and the login startup controller rejects a request without the correct token. The storefront has no such mechanism.

Why are admin and catalog models with the same name different classes?

The trickiest thing for a newcomer is identical routes. Both the admin panel and the storefront have a catalog/product model, but these are two different files with different logic.

  • admin/model/catalog/product.php is CRUD: addProduct(), editProduct(), deleteProduct(), and fetching raw data for the edit form without considering status, availability date, or customer groupA segment of buyers (retail, wholesale) with different prices and terms. More in the glossary →.
  • catalog/model/catalog/product.php is read-only for the customer: it takes into account product status, store assignment, customer group, promotions and discounts, and returns the rating and the ready price with the special price.

Calling $this->load->model('catalog/product') in the admin panel will always load the admin model, and in the storefront the storefront model. The Loader cannot load a model from the "other" application. So if you need a function in the storefront that exists only in the admin model, the correct approach is to write your own model in the catalog part of your module, rather than including the admin file via require. Otherwise you'll get a class name conflict: in OpenCart 3.0 both models are called ModelCatalogProduct, and in 4.1 they differ only by namespace.

The shared layer between the applications is the database and the settings table. A module stores its parameters in the admin panel via model_setting_setting->editSetting(), and the storefront reads them via $this->config->get(), because store settings are loaded into the config at the start of the request. The admin panel takes store settings from store_id 0, while the storefront determines the store by URL.

How is a module split into two halves in 3.0 and 4.1?

Almost every extension has two parts: the admin part (settings form, installation, uninstallation, permissions) and the storefront part (what the customer sees). The difference between versions lies in where these parts are located.

OpenCart 3.0

Module files are embedded into the core tree:

admin/controller/extension/module/hello.php
admin/language/en-gb/extension/module/hello.php
admin/view/template/extension/module/hello.twig
catalog/controller/extension/module/hello.php
catalog/language/en-gb/extension/module/hello.php
catalog/view/theme/default/template/extension/module/hello.twig

The route is the same in both applications, extension/module/hello, but different code runs depending on which index.php the request came through.

OpenCart 4.1

The extension lives in its own extension/<vendor>/ directory, and inside it there are again two applications:

extension/myvendor/admin/controller/module/hello.php
extension/myvendor/admin/language/en-gb/module/hello.php
extension/myvendor/admin/view/template/module/hello.twig
extension/myvendor/catalog/controller/module/hello.php
extension/myvendor/catalog/language/en-gb/module/hello.php
extension/myvendor/catalog/view/template/module/hello.twig

The route has the form extension/myvendor/module/hello, and the class namespace explicitly names the application:

Example

namespace Opencart\Catalog\Controller\Extension\Myvendor\Module;

class Hello extends \Opencart\System\Engine\Controller {
    public function index(array $setting = []): string {
        $this->load->language('extension/myvendor/module/hello');

        $data['title'] = $setting['name'] ?? $this->language->get('heading_title');
        $data['enabled'] = (bool)$this->config->get('module_hello_status');

        return $this->load->view('extension/myvendor/module/hello', $data);
    }
}

The admin controller of the same module has the namespace Opencart\Admin\Controller\Extension\Myvendor\Module. In OpenCart 4.1 the method in the route is separated by a dot (.save); the old separator | is automatically replaced with a dot. In 3.0 a slash is used (extension/module/hello/save).

The manager sees the admin part of the module in the "ExtensionsAdditional functionality: a module, a payment or shipping method, a feed, or a theme. They are managed in "Extensions → Extensions." More in the glossary → → Extensions" section, by selecting the extension type "Modules": here the module is installed and its settings form is opened. When installed, OpenCart automatically grants the current user's group permissions for the module's route.

Module list: the «Install» and «Edit» buttons open the admin part of the extension
Module list: the «Install» and «Edit» buttons open the admin part of the extension · OpenCart 4.1.0.4

How are events and access rights tied to a specific application?

The event system is shared, the event table is one for both applications, but each trigger starts with the application name: catalog/… or admin/…. The event startup controller in each application registers only those records whose trigger has its prefix, and the prefix itself is stripped off. That is, an event with the trigger catalog/model/checkout/order/addOrderHistory/after (3.0) or catalog/model/checkout/order.addHistory/after (4.1) will fire only in the storefront, and admin/view/catalog/product_form/before only in the admin panel. The action (action) specifies a route relative to the same application, so for an event with the catalog/ prefix the handler must be located in the catalog part of the module.

The event is usually registered in the admin method install():

Example

// OpenCart 4.1, адмінський контролер модуля
$this->load->model('setting/event');

$this->model_setting_event->addEvent([
    'code'        => 'hello_order_notify',
    'description' => 'Сповіщення після зміни статусу замовлення',
    'trigger'     => 'catalog/model/checkout/order.addHistory/after',
    'action'      => 'extension/myvendor/module/hello.notify',
    'status'      => 1,
    'sort_order'  => 0
]);

In OpenCart 3.0 the same model method accepts separate arguments: code, trigger, action, status, and sort order. Registered events are visible in the "Extensions → Events" section, where they can also be temporarily disabled during debugging.

Event list: the trigger column shows the catalog/ or admin/ application prefix
Event list: the trigger column shows the catalog/ or admin/ application prefix · OpenCart 4.1.0.4

Access rights exist only in the admin panel. For each user group, lists of routes are defined with "access" and "modify" permissions, and the startup controller checks the current route before execution. They are edited in "System → Users → User Groups". If after manually copying module files you get "Permission denied", you should usually check this exact list.

User group form: admin route lists with access and modify permissions
User group form: admin route lists with access and modify permissions · OpenCart 4.1.0.4

Where do admin and catalog intersect, and what are the pitfalls?

Despite the separation, the applications have several points of contact, and this is where modules most often break.

  • Database and settings. Fully shared. The admin panel writes, the storefront reads. Module settings keys must match in both parts down to the character, including the prefix module_, payment_, and so on.
  • Session. Both applications use the same session mechanism and drivers from system/, but they store different data in it: the admin panel stores user_id and user_token, the storefront stores customer and cart data. Details are in the article about OpenCart sessions and cache.
  • Cache and storage. The cache directory is shared. Clearing the cache in the admin panel affects the storefront, and this is often needed after changing settings that the storefront caches. The contents of the directory are covered in the article about the storage directory.
  • Links between applications. The admin panel builds storefront URLs via the HTTP_CATALOG constant (or the store URL from settings), not via $this->url->link(), because the latter generates links to the current application.
  • Images. Both applications work with the same image/ directory, but resizing for the storefront is done by the storefront tool/image model, while the admin panel has its own model with the same name.
  • Order editing. The admin panel does not recalculate the cart itself but calls the API in the storefront, because all cart, shipping, and totals logic lives in the catalog. Therefore a shipping or totals module that works in the storefront must also work correctly in this scenario.

A separate note on OCMOD: modifications are applied to files by their full path, so a single XML can modify both admin/ and catalog/ at the same time. This is convenient, but during a core update you'll need to check both halves.

Summary

  • Admin and catalog are two independent MVC applications with their own index.php, config.php, config in system/config/, startup actions, controllers, models, languages, and templates.
  • Only the system/ core, the database, settings, cache, and the images directory are shared.
  • Identical routes in the two applications are different classes with different logic. The Loader does not load code from the "other" application.
  • A module almost always consists of two parts: in OpenCart 3.0 they are spread across admin/ and catalog/, and in OpenCart 4.1 they are gathered in extension/<vendor>/admin and extension/<vendor>/catalog.
  • The event trigger determines the application by its prefix, and access rights exist only in the admin panel.

Next, it's worth walking through the path of a single request in detail and seeing how Request and Response handle input data and output. The remaining materials of this section are collected on the page Architecture and files of OpenCart.

FAQ

Can the admin run on a separate subdomain?
Yes. It's enough for the subdomain to point to the admin folder and for admin/config.php to have correct HTTP_SERVER and HTTP_CATALOG values. The code stays the same; only the URLs in the config change.
How can you check from the storefront whether the current visitor is logged in to the admin?
There is no built-in user object in the storefront. So this check is done with custom code, using session data or a separate token. Do it carefully and never perform admin actions in the storefront based on it.
Is the catalog part needed if the module only works in the admin?
No. A report, import or manager tool may consist only of admin files. The catalog part is needed only if the module displays something to customers or reacts to storefront events.
Why does the storefront show the old template after updating module files?
Most often the cause is the Twig cache, the data cache or outdated modifications. Refresh the modifications and clear the cache in the admin. In 3.0, also check that the template is in the active theme folder, not only in default.
Does the OpenCart 4.1 cron work in the admin or the storefront?
Scheduled tasks are configured in the admin but run separately via the server's cron. So the task code must not rely on a session or a logged-in user.
Was this article helpful?
Thanks for your feedback!

Related articles

Didn't find the answer?

Ask the community: OpenCart developers answer on the forum.

Ask on the forum
We use cookies

We use cookies and similar technologies to improve your experience, analyse traffic, and show personalised ads. Read our Cookie Policy for details.