stave.dev

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.

/ttrpg/characters/view/5
→
Ttrpg module
→
Characters submodule
→
view('5')

Directory Structure

A submodule is at {Module}/submodule/{Child}/{Child}.php:

modules/Ttrpg/ Ttrpg.php view/ frame/ assets/ submodule/ Characters/ Characters.php view/ assets/ model/ Hexmap/ Hexmap.php view/ assets/

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');
    }
}
Namespace Must Be the Parent

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:

  1. URL /ttrpg/characters/view/5 arrives
  2. Framework loads Ttrpg module and calls characters('view', '5')
  3. Ttrpg has no characters() method — __call() fires
  4. Framework finds submodule/Characters/Characters.php
  5. The first argument becomes the endpoint, index when there is none, with hyphens to camelCase
  6. 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)

Infinite Nesting

A submodule extends Module, so its own __call() does the same lookup one level down. Submodules can hold submodules to any depth:

modules/Ttrpg/ submodule/ Characters/ Characters.php ← namespace Zero\Module\Ttrpg submodule/ Inventory/ Inventory.php ← namespace Zero\Module\Ttrpg\Characters

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.

__call() endpoints cannot carry their own attributes

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:

  1. Check submodule: modules/Ttrpg/submodule/Characters/frame/
  2. Check parent module: modules/Ttrpg/frame/
  3. 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():

modules/Ttrpg/submodule/Hexmap/assets/
→
/assets/ttrpg/hexmap/

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)
Non-Convention Assets in Submodules

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.

When to Use Submodules

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.