Submodules
A module can hold other modules. The URL shifts down one level at each, to any depth.
What Are Submodules?
A submodule is a module that lives inside another module's submodule/ directory. It has its own controller, views, assets and models, and it uses its parent's frame unless it has one of its own. This lets you break a large feature into pieces without filling the top-level modules/ directory.
Directory Structure
A submodule is at {Module}/submodule/{Child}/{Child}.php:
core/modules/Module/mkmod Ttrpg --submodule=Characters writes one for you. See Creating Modules.
Namespace Convention
A submodule's namespace is its parent's full class name, with no submodule segment. It extends \Zero\Core\Module, not its parent:
<?php
namespace Zero\Module\Ttrpg;
class Characters extends \Zero\Core\Module {
public function index() {
$this->respond($this->viewPath . 'index.php');
}
public function view($id) {
$this->data = ['character_id' => $id];
$this->respond($this->viewPath . 'view.php');
}
}
The namespace for Characters is Zero\Module\Ttrpg, not Zero\Module\Characters. Module::__call() builds the class name as the parent's class plus \Characters, so any other namespace is a class that does not exist.
Compare this to the parent module:
<?php
namespace Zero\Module;
class Ttrpg extends \Zero\Core\Module {
public function index() {
$this->respond($this->viewPath . 'index.php');
}
// No characters() method needed - the framework
// automatically delegates to the submodule
}
URL Routing Delegation
The framework first tries the matching method on the parent module. When the parent has none, Module::__call() in core/Module.php looks for a submodule of that name:
- URL
/ttrpg/characters/view/5arrives - Framework loads
Ttrpgmodule and callscharacters('view', '5') Ttrpghas nocharacters()method —__call()fires- Framework finds
submodule/Characters/Characters.php - The first argument becomes the endpoint,
indexwhen there is none, with hyphens to camelCase - Routing shifts:
Characters::view('5')is called
| URL | Resolution |
|---|---|
/ttrpg |
Ttrpg::index() |
/ttrpg/characters |
Characters::index() (submodule) |
/ttrpg/characters/view/5 |
Characters::view('5') (submodule) |
/ttrpg/hexmap/dm |
Hexmap::dm() (submodule) |
- A method beats a submodule. If the parent has a public
characters()method, it answers and the submodule is never looked for. - A missing required argument is a 400, as it is for a top-level module.
- A submodule endpoint needs a method. The view-file fallback described in Routing reads the first two URL segments, so
/ttrpg/characters/sheetwith nosheet()method does not render the submodule'sview/sheet.php. - Errors are rendered by the top-level module. An
HTTPErrorthrown in a submodule is caught inApplication::run(), which holds the parent, so the page uses the parent'sview/error{code}.php. See Error Handling.
Infinite Nesting
A submodule extends Module, so its own __call() does the same lookup one level down. Submodules can hold submodules to any depth:
The URL /ttrpg/characters/inventory/view/42 resolves to Inventory::view('42') through two levels of delegation.
Attributes on Submodules
Attributes declared inside modules/{Parent}/submodule/{Child}/ are enforced exactly like those on a top-level module. Application::checkAttributes() is public and static, and each dispatcher calls it for its own (class, endpoint) pair — Application::run() for the top-level module, then Module::__call() for the submodule, before the submodule is constructed.
The gates are therefore additive down the nesting chain. A request to /ttrpg/hexmap/save runs the Ttrpg class attributes first (because Ttrpg has no hexmap() method), then the Hexmap class and save() attributes. Nothing is evaluated twice: class-level attributes are not inherited by PHP reflection, and a submodule extends Module rather than its parent module.
Attributes resolve against the URL endpoint name, and an endpoint dispatched through __call() has no ReflectionMethod of its own. Attributes declared on __call() itself are read on its behalf, but they are additive on top of the class-level ones — nothing can re-open a gate the class closed. A class whose __call() serves anonymous traffic must therefore gate per method rather than at class level. They also run when the URL segment resolves to a submodule, since the parent has no method of that name either.
Attributes has the full list and the class-versus-method rules.
Frame Inheritance
A submodule with no frame/ directory uses its parent's. Module::defineBasePaths() walks up the directory tree and takes the nearest ancestor that has one:
- Check submodule:
modules/Ttrpg/submodule/Characters/frame/ - Check parent module:
modules/Ttrpg/frame/ - Fall back to app frame:
app/frontend/frame/
The walk picks one directory, not one file at a time. A submodule frame/ holding only head.php uses that head, and gets the other pieces from the site's frame, not from its parent's. Views & Responses has the lookup for each piece.
Only the frame is inherited. A submodule has its own view/, model/ and assets/. Its wording and config are the top-level module's: strings/, config.ini and config/ are read from the module the URL names.
Asset Loading for Submodules
A submodule's assets/ folder is linked under its parent's public path by Module::__construct():
Auto-loading follows the same convention, scoped to the submodule. For the URL /ttrpg/hexmap/dm, the asset loader checks /assets/ttrpg/hexmap/css/ for:
| File | Loaded Because |
|---|---|
hexmap.css |
Matches the submodule name (loads for all Hexmap endpoints) |
dm.css |
Matches the endpoint name (loads only for /ttrpg/hexmap/dm) |
- The same
$this->getStylesheets()and$this->getScripts()calls work in a frame a submodule inherits: they read the submodule's own asset path. - Asset folders are not inherited. A submodule with no
assets/loads no module CSS or JS, even when its parent has some. - The endpoint file of a submodule is named after the method:
/ttrpg/hexmap/save-maploadssaveMap.css.
If a submodule needs assets that don't follow the naming convention (like main.css or a vendor library), hardcode them in the submodule's frame/head.php alongside the $this->getStylesheets() call. Assets has the full convention.
When the Parent Is Off
A submodule follows its parent. When the parent is switched off in app/modules.ini, its controller is never loaded, so nothing under it can be reached, and the public asset links into the parent's folder, its submodules' included, are removed. See Bundled Modules.
Submodules work best when features are logically children of a parent (Characters belong to a TTRPG app). If two modules are peers that share nothing, keep them as top-level modules instead.