docs/developer-guide/startups.md
The startup system allow developers process code at very early stage when opening a page, it is useful for example to initialize some library for further use, handle seo related data, etc. This guide explains how to work with startups in OpenCart with practical examples.
Step 1: Create the Startup Class
Create a PHP class for your extension. For example, place the following code in extension/test_module/catalog/controller/startup.php:
<?php
namespace Opencart\Catalog\Controller\Extension\TestModule;
class Startup extends \Opencart\System\Engine\Controller {
public function index(): void {
// Process your customizations here
$this->log->write('My startup has been successfully initialized!');
}
}
Step 2: Register the Startup
Startups are typically registered in the install() method of your extension. For programmatic registration, use the setting/startup model.
Add the following code to your module’s controller:
<?php
namespace Opencart\Admin\Controller\Extension\TestModule\Module;
class TestModule extends \Opencart\System\Engine\Controller {
public function install(): void {
// Load the startup model
$this->load->model('setting/startup');
// Register the startup
$this->model_setting_startup->addStartup([
'code' => 'my_startup',
'action' => 'catalog/extension/test_module/startup',
'description' => 'My custom startup',
'sort_order' => 1,
'status' => true
]);
}
public function uninstall(): void {
// Remove the startup on uninstall
$this->load->model('setting/startup');
$this->model_setting_startup->deleteStartupByCode('my_startup');
}
}
Step 3: Test the Startup
Extensions > Installer).Extensions > Extensions).Extensions > Startups.System > Maintenance > Error logs) to verify that your custom log message appears.