# OpenCart framework.php: request flow from index.php to controller

> We break down how a request in OpenCart 3.0 and 4.x passes through index.php, startup.php and framework.php, where services are created and how the controller is chosen.

Джерело: https://opencartcode.com/en/wiki/dev-architecture/opencart-frameworkphp
OpenCart: 3.0, 4.x
Оновлено: 2026-11-24

Every request to the OpenCart [storefront](https://opencartcode.com/en/wiki/glossary/e-commerce-glossary#storefront) or admin follows the same path. First, `index.php` loads `config.php`. Then `system/startup.php` prepares the PHP environment and autoloading. After that, `opencart framework.php`, that is the `system/framework.php` file, builds the Registry with all services, runs the pre-actions, calls the required [controller](https://opencartcode.com/en/wiki/glossary/e-commerce-glossary#controller), and returns the response. If you understand this chain, you can easily find where to hook in a module, why an event does not fire, and where a white page comes from.

## What does index.php do, and how does it differ in 3.0 and 4.x?

The root `index.php` is very short. It defines the constant `VERSION` and loads `config.php`. Then it checks whether `DIR_APPLICATION` is defined. If not, the store is considered not installed, and the browser is redirected to `install/index.php`. That is why an empty or broken `config.php` on a live site immediately opens the installer. This is the first thing to check when a store suddenly "asks" to be installed again.

`index.php` · OpenCart 3.0.5.1 · lines 17–20

```php
// Startup
require_once(DIR_SYSTEM . 'startup.php');

start('catalog');
```

**OpenCart 3.0:** after `startup.php`, the function `start()` is called with the application configuration name. This function is declared in `startup.php` and includes `framework.php` inside it. As a result, the framework code runs in the function's scope, and the variable `$application_config` becomes its argument. The admin has its own `admin/index.php` with its own `config.php`, and it runs the same sequence for the admin configuration.

`index.php` · OpenCart 4.1.0.4 · lines 17–21

```php
// Startup
require_once(DIR_SYSTEM . 'startup.php');

// Framework
require_once(DIR_SYSTEM . 'framework.php');
```

**OpenCart 4.x:** the `start()` function no longer exists. `framework.php` is loaded directly, and the application is defined by the constant `APPLICATION` (`Catalog` or `Admin`), which is set by `config.php`. It determines the controller namespace (`Opencart\Catalog\...`) and which configuration file is loaded. Where these files live in both versions is described in the article [OpenCart 3.0 and 4.x directory map](https://opencartcode.com/en/wiki/dev-architecture/opencart-file-structure).

## Why do we need system/startup.php?

`startup.php` normalizes the environment and knows nothing about the store. In both versions it does the following:

- enables `error_reporting(E_ALL)`;
- requires PHP 8.1+ (current 3.0.5.x and 4.1.0.x releases);
- sets the UTC time zone if it is not set in php.ini;
- for IIS, fills in `DOCUMENT_ROOT`, `REQUEST_URI`, and `HTTP_HOST`;
- detects HTTPS and stores the result in `$_SERVER['HTTPS']` as a boolean value.

The HTTPS check takes into account the `X-Forwarded-Proto` and `X-Forwarded-SSL` headers. This matters if the store sits behind Cloudflare, an nginx proxy, or a hosting load balancer. When a proxy does not pass these headers, OpenCart will consider the connection insecure. The consequences vary: mixed content, redirect loops, and, in 4.x, a session cookie without the `secure` flag.

Beyond this point, the versions differ significantly.

| What | OpenCart 3.0 | OpenCart 4.x |
| --- | --- | --- |
| Autoloading | `library()` function via `spl_autoload_register`, looks for classes in `system/library/` | `Autoloader` class with `system/engine/autoloader.php`, namespaces are registered already in framework.php |
| Core (engine) | Action, Controller, Event, Router, Loader, Model, Registry, Proxy are included explicitly | loaded by the autoloader on demand |
| OCMOD | `modification()` function replaces the path with a file from the modification cache | no such function in startup.php |
| Composer vendor | `storage/vendor/autoload.php` in startup.php | the same file, but in framework.php |
| Helpers | general.php, utf8.php | general.php, validation.php |

`system/startup.php` · OpenCart 3.0.5.1 · lines 89–105

```php
// Engine
require_once(modification(DIR_SYSTEM . 'engine/action.php'));
require_once(modification(DIR_SYSTEM . 'engine/controller.php'));
// ...
require_once(modification(DIR_SYSTEM . 'engine/proxy.php'));

// Helper
require_once(DIR_SYSTEM . 'helper/general.php');
require_once(DIR_SYSTEM . 'helper/utf8.php');

function start($application_config) {
	require_once(DIR_SYSTEM . 'framework.php');	
}
```

> OpenCart 3.0: `framework.php` and the helpers are included without `modification()`. Therefore, an OCMOD modification that changes `system/framework.php` will appear in the modification cache but will not be executed. Extend startup logic through pre-actions, events, or core files that pass through `modification()`.

## In what order does framework.php create services?

`framework.php` works as a dependency assembler. It creates objects and puts them into the Registry, and then controllers and models receive them as `$this->config`, `$this->db`, `$this->session`. The mechanism itself is discussed in detail in the article about the Registry. What matters here is the order: a service that has not yet been created is unavailable both in the error handler and in an early event.

1. **Registry.** In 4.x, the autoloader is put into it right away. Before that, the `Opencart\Catalog` (or `Admin`), `Opencart\Extension`, and `Opencart\System` namespaces are registered.
2. **Config.** First `system/config/default.php` is loaded, then the application file: `catalog.php` or `admin.php`. Store settings from the `setting` table do not exist yet at this point.
3. **Log and error handler.** In 4.x, an exception handler is also set up.
4. **Event.** Events hard-coded in the `action_event` config are registered.
5. **Loader, Request, Response.** In 4.x, `Factory` appears between Event and Loader.
6. **DB**, if `db_autostart` is enabled.
7. **Session.** In 3.0, the object is always created but only starts when `session_autostart` is set. In 4.x, both creation and start happen only when `session_autostart` is set.
8. **Cache, Language, Url, Document.** In 4.x, `Template` is created before them.
9. **Autoload and pre-actions.** In 3.0, config/language/library/model files are first loaded from the autoload arrays in the config, and then pre-actions run. In 4.x, there are no autoload arrays in framework.php, so pre-actions run immediately.
10. **Dispatch** of the controller and `$response->output()`.

```mermaid
flowchart TD
  A["index.php"] --> B["config.php"]
  B --> C["system/startup.php"]
  C --> D["framework.php: Registry, Config"]
  D --> E["Log, Event, Loader"]
  E --> F["Request, Response, DB, Session"]
  F --> G["Cache, Language, Url, Document"]
  G --> H["Pre-actions (startup/*)"]
  H --> I["Event controller/.../before"]
  I --> J["Route controller"]
  J --> K["Event controller/.../after"]
  K --> L["Response::output()"]
```

This leads to a simple rule. Values such as `config_name` or `config_language` appear in `$this->config` only after a pre-action that reads settings from the database. You cannot rely on them in configuration files or very early events. How file configs and the settings table work together is covered in the article about config.php and system/config.

## How does framework.php select and run a controller?

### OpenCart 3.0: Router

In 3.0, routing is handled by the `Router` class. It receives the pre-actions via `addPreAction()` and then calls `dispatch()` with two `Action` objects: the router (`action_router`) and the error controller (`action_error`).

`system/framework.php` · OpenCart 3.0.5.1 · lines 168–182

```php
// Route
$route = new Router($registry);

// Pre Actions
if ($config->has('action_pre_action')) {
	foreach ($config->get('action_pre_action') as $value) {
		$route->addPreAction(new Action($value));
	}
}

// Dispatch
$route->dispatch(new Action($config->get('action_router')), new Action($config->get('action_error')));

// Output
$response->output();
```

Pre-actions are described in `system/config/catalog.php` and `admin.php`. These are controllers from the `controller/startup/` directory: loading store, language, and currency settings, registering events from the database, parsing SEO URLs, maintenance mode, and so on. If a pre-action returns a new `Action`, the Router executes it instead of the requested route. This is how maintenance mode replaces any page with its placeholder.

### OpenCart 4.x: a loop directly in framework.php

In 4.x, there is no Router class. Routing happens openly, in `framework.php` itself. First, the pre-actions run. The first one that returns `Action` stops the iteration and sets the route. Then the route is taken from `route` in GET or from `action_default`. After that, the `before` event fires, the execution loop runs, and then the `after` event fires.

`system/framework.php` · OpenCart 4.1.0.4 · lines 272–292

```php
// Dispatch
while ($action) {
	// Execute action
	$output = $action->execute($registry, $args);

	// Make action a non-object so it's not infinitely looping
	$action = '';

	// Action object returned then we keep the loop going
	if ($output instanceof \Opencart\System\Engine\Action) {
		$action = $output;
	}

	// If action cannot be executed, we return the action error object.
	if ($output instanceof \Exception) {
		$action = $error;

		// In case there is an error we don't want to infinitely keep calling the action error object.
		$error = '';
	}
}
```

new Action('інший/маршрут'), and the loop continues with it. This is how an internal "forward" without an HTTP redirect is implemented. An error is considered to be a returned `\Exception`, for example when a class or method is not found. In that case, `action_error` is executed once, and the `$error` variable is reset to avoid an infinite loop.

## What other 4.x differences should a module developer know about?

The biggest problems when porting modules come from the route separator and the protection of "private" methods.

`system/framework.php` · OpenCart 4.1.0.4 · lines 138–142

```php
// Compatibility
if (isset($request->get['route'])) {
	$request->get['route'] = str_replace('|', '.', $request->get['route']);
	$request->get['route'] = str_replace('%7C', '|', (string)$request->get['route']);
}
```

A controller method in a route is separated by a dot: `extension/mymodule/module/feed.save`. The old format with a vertical bar `|` from early 4.0 releases is automatically converted to a dot. So old links still work, but write with a dot in new code.

`system/framework.php` · OpenCart 4.1.0.4 · lines 250–253

```php
// To block calls to controller methods we want to keep from being accessed directly
if (str_contains($route, '._')) {
	$action = new \Opencart\System\Engine\Action($config->get('action_error'));
}
```

Methods whose names start with an underscore cannot be called via URL. A request to them will get an error page. Internal controller methods, for example render fragments for AJAX that are called only through `$this->load->controller()`, should be named with `_`, and they cannot be reached from outside.

Other notable changes:

- **Exception handler.** If `error_display` is disabled, on a fatal error or an uncaught exception the visitor is redirected to `error_page`, and the full backtrace is written to the log.
- **Headers.** The response receives headers from the `response_header` config and CORS headers (`Access-Control-Allow-Origin: *` and others). Keep this in mind if you configure CSP or CORS at the server level, so you do not duplicate headers.
- **Database time zone.** In 3.0's framework.php, right after connecting to the database, `config_timezone` is read and `time_zone` is synchronized with MySQL. In 4.x's framework.php, this block does not exist, because this work has been moved to later startup stages.
- **Session cookie.** It is set with `httponly`, `samesite`, and `secure` depending on HTTPS. There is a separate article on admin security overall: [how to protect the OpenCart panel from hacking](https://opencartcode.com/en/blog/bezpeka-adminki-opencart-zaxist-paneli-vid-zlamu).

## How do you hook into this chain without editing the core?

Editing `framework.php` or `startup.php` directly is a bad idea: the first update will wipe the changes, and in 3.0, OCMOD is not even applied to framework.php. There is almost always a better way.

1. **Code before or after a specific controller.** Use the `controller/маршрут/before` and `controller/маршрут/after` events. In 4.x, they are called directly in framework.php around the main route and receive `$route`, `$args`, and `$output` by reference. This means you can replace the route or change the finished HTML. Registered events can be viewed in the "Extensions → Events" section.
2. **Code on every request.** In 4.x, there is an "Extensions → Startup" section for this. [Extension](https://opencartcode.com/en/wiki/glossary/e-commerce-glossary#extension) startup controllers are registered there and run at startup alongside the system pre-actions. In 3.0, there is no such section, and usually an event on an early route or an OCMOD on the startup controller is used instead.
3. **Error diagnostics.** Enable logging and, if needed, error output in "System → Settings" (System → Settings), "Server" tab (Server). Keep in mind that before the settings are loaded from the database, the `error_display` / `error_log` values from the `system/config` files apply. So an error in the startup itself may not show up, even though output is enabled in the admin.

List of events: trigger, action, and status of each registered event · OpenCart 4.1.0.4

Extensions → Startup in 4.x: controllers that run on every request · OpenCart 4.1.0.4

Server tab: error output and logging · OpenCart 4.1.0.4

> A blank white page without text in 4.x often means this: an exception occurred, `error_display` is disabled, and the redirect to `error_page` did not work because the headers had already been sent. Check the [error log](https://opencartcode.com/en/wiki/glossary/e-commerce-glossary#error-log) in "System → Maintenance → Error Logs" (System → Maintenance → Error Logs) or the file in `storage/logs/`.

If the stack of modifications and events has become so tangled that sorting it out yourself is expensive, you can turn to [OpenCart refinement and bug fixing](https://opencartcode.com/en/marketplace/product/opencart-fixes). But in most cases, the log and an understanding of which stage of framework.php the failure occurred at are enough. Unfamiliar terms are easy to check against the [OpenCart glossary of terms](https://opencartcode.com/en/wiki/glossary/e-commerce-glossary).

## Summary

- The order is always this: `index.php` → `config.php` → `system/startup.php` → `system/framework.php` → pre-actions → controller → `Response::output()`.
- `startup.php` prepares the environment: [PHP version](https://opencartcode.com/en/wiki/glossary/e-commerce-glossary#php-version), HTTPS, autoloading. `framework.php` builds the Registry and executes the route.
- OpenCart 3.0: `start('catalog')`, the `Router` class, OCMOD via `modification()`, but framework.php itself is not modified by it.
- OpenCart 4.x: the `APPLICATION` constant, namespaces, an open dispatch loop, routes with a dot, methods with `_` closed to URLs, an exception handler with a redirect to `error_page`.
- Extend startup through events and startup controllers, not through core edits.

Next, it makes sense to look at how services from the Registry get into a controller and how the Loader works. These and related topics are collected in the section [Architecture and files of OpenCart](https://opencartcode.com/en/wiki/dev-architecture). The overall picture of the strengths and weaknesses of this architecture is given in the overview of [OpenCart advantages and disadvantages](https://opencartcode.com/en/wiki/start/opencart-pros-and-cons).

## FAQ

**Can I run OpenCart's framework.php from my own CLI script?**

Technically yes: define the required constants via config.php, include startup.php and framework.php, and pass the route in $_GET['route']. But it's simpler to use the built-in cron in 4.x or a separate controller called via curl.

**Why does my controller in 4.x show an error even though the file and class exist?**

Most often the namespace or class name doesn't match the file path, so the autoloader can't find it. Another common cause: the method is called via "|" instead of a dot in old JS code, or the method name starts with an underscore.

**Where are pre-actions set, and can I add my own?**

System pre-actions are listed in action_pre_action in system/config/catalog.php and admin.php. For your own logic on every request in 4.x, use Extensions → Startup, and in 3.0 use events or OCMOD on the startup controller, not by editing core config.

**Does the number of events and pre-actions affect store speed?**

Every event and every pre-action runs on each request to the matching route, so heavy database queries in them noticeably slow the site. Keep such handlers lightweight and cache results via $this->cache.
