VOOZH about

URL: https://deepwiki.com/MahoCommerce/maho/12.1-creating-custom-modules

⇱ Creating Custom Modules | MahoCommerce/maho | DeepWiki


Loading...
Last indexed: 15 May 2026 (ea8ab8)
Menu

Creating Custom Modules

Purpose and Scope

This document provides a technical guide for developing custom modules in Maho. It covers module structure, declaration, configuration, and the implementation of core components (models, blocks, controllers, helpers). For information about extending existing functionality through event observers, see Event Observers and Hooks For creating custom CLI commands, see Custom CLI Commands


Module Structure Overview

Maho modules follow a standardized directory structure that organizes code by functional component. Each module is self-contained and can be enabled/disabled independently.


Sources: app/code/core/Mage/Core/Model/Config.php15-71 app/code/core/Mage/Core/Model/App/Area.php15-23


Module Declaration

Every module must be declared in an XML file located in app/etc/modules/. This file controls module loading order and enables/disables the module.

Declaration File Structure

Create app/etc/modules/Namespace_ModuleName.xml:


Key Elements

ElementDescription
<active>true or false - enables/disables the module
<codePool>core, community, or local - determines directory location
<depends>Lists module dependencies - controls load order

Module Load Order

Maho loads modules in a specific sequence to resolve dependencies. Core modules have predefined order indices maintained in Mage_Core_Model_Config app/code/core/Mage/Core/Model/Config.php15-71 The configuration system merges these XML files into a global configuration object during initialization app/code/core/Mage/Core/Model/Config.php272-279


Sources: app/code/core/Mage/Core/Model/Config.php15-71 app/code/core/Mage/Core/Model/Config.php272-279


Module Configuration (config.xml)

The config.xml file is the heart of every module, defining its structure, components, and integration points. Create this file at app/code/local/Namespace/ModuleName/etc/config.xml.

Minimal config.xml


Configuration Scopes

Maho configuration is organized into sections that control where components are available. These are managed by the Mage_Core_Model_App_Area class app/code/core/Mage/Core/Model/App/Area.php15-23

ScopePurposeCommon Uses
<global>Available everywhereModels, blocks, helpers, resource models
<frontend>Frontend area onlyFrontend routers, layout updates, translations
<adminhtml>Admin area onlyAdmin routers, menu items, ACL rules

Model Configuration Mapping

The following diagram bridges the XML configuration to the actual PHP class instantiation logic handled by the system.


Sources: app/code/core/Mage/Core/Model/Config.php112-116 app/code/core/Mage/Tag/Model/Tag.php38-74


Module Components

Models and Resource Models

Models contain business logic, while resource models handle database operations.

Model Class Structure

Models usually inherit from Mage_Core_Model_Abstract app/code/core/Mage/Tag/Model/Tag.php38 or Maho\DataObject app/code/core/Mage/Core/Model/Locale.php13


Resource Model Structure

Maho supports multiple database engines including MySQL, PostgreSQL, and SQLite lib/MahoCLI/Commands/DBQuery.php50-54 Resource models define the mapping between entities and tables. The system uses Mage_Core_Model_Config to retrieve connection details for these engines lib/MahoCLI/Commands/DBQuery.php45

Sources: app/code/core/Mage/Tag/Model/Tag.php38 app/code/core/Mage/Tag/Model/Tag.php71-74 lib/MahoCLI/Commands/DBQuery.php45-54

Model Factory Pattern

Maho uses factory methods defined in Mage to instantiate models, utilizing a class name cache for performance app/code/core/Mage/Core/Model/Config.php112-116


Sources: app/Mage.php459-474 app/code/core/Mage/Core/Model/Config.php112-116


Blocks

Blocks prepare data for templates and are referenced by aliases in layout XML. The Mage_Core_Model_Layout class manages the block registry and instantiation app/code/core/Mage/Core/Model/Layout.php23-27

Block Factory Mapping


Sources: app/code/core/Mage/Core/Model/Layout.php211-224 app/code/core/Mage/Core/Model/Layout.php62


Helpers

Helpers provide utility functions and are often used for translations.

Accessing Helpers


Sources: app/Mage.php539-553


Controllers

Controllers handle HTTP requests. They typically reside in the controllers/ directory of a module.

Router Configuration

Routers are configured in config.xml. The Mage_Core_Model_App_Area handles the initialization of different areas (frontend vs adminhtml) which dictates which routers are active app/code/core/Mage/Core/Model/App/Area.php15-18

Sources: app/code/core/Mage/Core/Model/App/Area.php15-18 app/code/core/Mage/Core/Model/App/Area.php91-114


System Configuration

System configuration is defined in system.xml and allows settings to be managed in the Admin Panel.

Configuration Pathing

Configuration values are retrieved using paths like general/locale/code app/code/core/Mage/Core/Model/Locale.php53


Sources: app/code/core/Mage/Core/Model/Locale.php107 app/Mage.php277-280


Modern Event Observers (Attributes)

In Maho, observers are registered using PHP 8.3 attributes directly on the observer class methods. This replaces the traditional XML-based registration in config.xml.


This pattern is used extensively in core for inventory management, gift messages, and activity logging app/code/core/Mage/CatalogInventory/Model/Observer.php44-45 app/code/core/Mage/GiftMessage/Model/Observer.php20-21 app/code/core/Maho/AdminActivityLog/Model/Observer.php48-49

Sources: app/code/core/Mage/CatalogInventory/Model/Observer.php44-45 app/code/core/Mage/GiftMessage/Model/Observer.php20-21 app/code/core/Maho/AdminActivityLog/Model/Observer.php48-49 app/code/core/Mage/Tag/Model/Tag.php194-195


Translation Files

Maho uses CSV files for translations. Modules initialize their translations through the area model during the request lifecycle app/code/core/Mage/Core/Model/App/Area.php131-135

Translation Initialization


Sources: app/code/core/Mage/Core/Model/App/Area.php131-135


Key Takeaways

ComponentFactory MethodBase ClassPurpose
ModelMage::getModel()Mage_Core_Model_AbstractBusiness logic
Resource ModelMage::getResourceModel()Mage_Core_Model_Resource_Db_AbstractDatabase operations
BlockMage::app()->getLayout()->createBlock()Mage_Core_Block_AbstractView logic
HelperMage::helper()Mage_Core_Helper_AbstractUtilities

Sources: app/Mage.php459-553 app/code/core/Mage/Core/Model/Layout.php211-224