Modules

A module gives a feature one place to register the pieces it needs. Use one when pages and services belong together and you want the application to install them as a unit. For a single page that already fits the application's existing directories and providers, the regular pages() and services() hooks are enough.

Group the product catalogue in a module

The catalogue walkthrough registers a ProductService binding through an application provider. A catalogue module can own that same binding and register ProductPage together with it. The application then chooses whether to install the catalogue and where its URLs start.

Keep the earlier Product, ProductService, and SampleProductService classes in app/Catalog. Move the service-backed product page and its template into app/Catalog/UI/Pages. In ProductPage.php, change the namespace to App\Catalog\UI\Pages and import App\Pages\AppPage so the page still uses the shared layout. Keep #[PageRoute("/products/{id}")] on the class.

app/Catalog/
    CatalogModule.php
    Product.php
    ProductService.php
    SampleProductService.php
    UI/Pages/
        ProductPage.php
        ProductPage.atom.html

Create the module class beside the service. bind() tells Atom which service to inject and pages() tells it where to discover the feature's page routes.

app/Catalog/CatalogModule.php
<?php

namespace App\Catalog;

use Atom\Module\ModuleContext;
use Atom\Module\ModuleInterface;

final class CatalogModule implements ModuleInterface
{
    public function register(ModuleContext $context): void
    {
        $context->bind(ProductService::class)->to(SampleProductService::class);
        $context->pages(__DIR__ . "/UI/Pages");
    }
}

Install the catalogue in the application

Import App\Catalog\CatalogModule in app/Application.php. Add the module inside the existing modules() hook. If you previously added CatalogServiceProvider to services() for this example, remove that provider registration because the module now supplies the binding.

app/Application.php (excerpt)
$modules->add(new CatalogModule(), "/catalog");

Atom combines the mount path /catalog with the page's route /products/{id}. Start the app and open /catalog/products/42 to see Desk lamp and In stock, or /catalog/products/43 to see Floor lamp and Sold out. The route still supplies id to get(), and the module binding supplies the constructor's ProductService. Omit the mount path to keep the original /products/42 URL.

Mount an editorial checklist

Package a publishing checklist with a small module. An editor can request the module's URL to discover where to fetch the current checklist. The application chooses the URL prefix when it installs the module.

In an Atom Starter project, create app/BlogTools/UI/Resources/checklist.txt with these two lines.

Confirm the headline.
Check the links.

Create app/BlogTools/BlogToolsModule.php beside the UI directory.

<?php

declare(strict_types=1);

namespace App\BlogTools;

use Atom\Http\Response;
use Atom\Module\ModuleContext;
use Atom\Module\ModuleInterface;
use Atom\Router\RouteEntry;

final class BlogToolsModule implements ModuleInterface
{
    public function register(ModuleContext $context): void
    {
        $context->resources("/resources", __DIR__ . "/UI/Resources");
        $checklist = $context->resourcePath("/resources", "checklist.txt");

        $context->route(RouteEntry::get(
            $context->mountedPath(),
            static fn(Response $response): Response => $response->json(["checklist" => $checklist])
        ));
    }
}

In app/Application.php, import App\BlogTools\BlogToolsModule and add this line inside the Starter's existing modules(ModuleRegistry $modules) method.

$modules->add(new BlogToolsModule(), "/tools/blog");

Run php atom serve from the project root. In a second terminal, request the module index, then fetch the resource URL from its checklist field.

curl -fsS http://127.0.0.1:8020/tools/blog
curl -fsS http://127.0.0.1:8020/tools/blog/resources/checklist.txt

The index returns /tools/blog/resources/checklist.txt as the checklist value. The second request prints the two checklist lines. To move the feature, change the mount path in app/Application.php to /editorial and request $site/editorial. The index now points to /editorial/resources/checklist.txt because mountedPath() and resourcePath() both use the registered prefix.

Register module features

Modules are registered after the injector and router are ready and before bootstrap() runs. registerModule() is still available when a module must be registered manually.

ModuleContext currently exposes:

$context->router;
$context->injector;
$context->components;
$context->bindings;
$context->basePath;

$context->mountedPath();
$context->mountedPath("/resources/app.css");
$context->resourcePath();
$context->resourcePath("/assets", "app.css");
$context->root();
$context->bind(SomeService::class)->toSelf()->singleton();
$context->route($entry);
$context->component("TagName", ComponentClass::class);
$context->component("Feature.AppShell", AppShell::class);
$context->commands(__DIR__ . "/Commands", __NAMESPACE__ . "\\Commands");
$context->jobs(FeatureJob::class);
$context->schedule(static function (Schedule $schedule): void {
    $schedule->command("feature:cleanup")->daily();
});
$context->pages(__DIR__ . "/UI/Pages", [
    ModulePageMiddleware::class,
]);
$context->resources("/resources", __DIR__ . "/UI/Resources");

Command directories registered through ModuleContext::commands() are added to the normal console discovery sources. Commands can extend Command or expose methods marked with #[ConsoleCommand]. Only installed modules contribute their commands.

final readonly class FeatureCommands
{
    #[ConsoleCommand("feature:publish", "Publish feature scaffolding")]
    public function publish(): int
    {
        return 0;
    }
}

Register module commands during normal application initialization, before ConsoleApplication is first resolved.

The module convention is:

Feature/
  UI/
    Pages/
    Components/
    Resources/

Resources are served through the framework static file handler. A resource directory registers one wildcard GET route under the given path. With a module base path of /tools/blog, this call:

$context->resources("/resources", __DIR__ . "/UI/Resources");

serves:

/tools/blog/resources/app.css

for:

UI/Resources/app.css

Use resourcePath() when a component or page needs to render a URL to a mounted resource:

$context->resourcePath(); // /tools/blog/resources
$context->resourcePath("/assets", "app.css"); // /tools/blog/assets/app.css

Registering the same resource route more than once returns the existing route instead of creating a duplicate.

Use root() when a module needs to register shared routes or resources outside its mounted path:

Client::resources($context->root());

The optional client module serves the Atom browser runtime and MorphDOM integration:

use Atom\Modules\Client\Client;

Client::resources($context);

Its default URLs are:

/atom/client/resources/atom.js
/atom/client/resources/morphdom.js
/atom/client/resources/atom-morphdom.js

Pages discovered through pages() are registered under the context base path.

Then a page declared with:

#[PageRoute("/settings")]

is served from:

/tools/blog/settings

Components can use dotted names to keep module UI tags grouped:

$context->component("BlogTools.AppShell", AppShell::class);

Components registered through a module context are private to that module. Every module receives a sealed child registry that falls back to application-global components. A local definition takes precedence over a global tag with the same name, without changing the application registry.

When a module provides several components, expose them as a reusable ComponentSet and import the set through the context:

public static function definitions(): ComponentSet
{
    return ComponentSet::from([
        "BlogTools.AppShell" => AppShell::class,
        "BlogTools.ArticleList" => ArticleList::class,
    ]);
}

public function register(ModuleContext $context): void
{
    $context->importComponents(BlogTools::definitions());
}

The set separates component definitions from where they are registered. Another module can import the same mappings without exposing those tags to the application.

Use an explicit export only when a module intentionally provides tags to ordinary application templates:

$context->exportComponent("BlogCard", BlogCard::class);
$context->exportComponents(BlogPublicComponents::definitions());

The Client module exports its tags for use in application templates. Feature modules such as Accounts and API Explorer keep their implementation components private.

Then a module page template can render:

<BlogTools.AppShell>
    <h1>{{ $this->title }}</h1>
</BlogTools.AppShell>

Module Route Scope

Every route, discovered page, page action, and new resource route registered through ModuleContext receives the same immutable ModuleScope metadata. A matched route exposes its owner directly:

$scope = $matchedRoute->moduleScope();

$scope?->id;
$scope?->module;
$scope?->basePath;
$scope?->components;

During dispatch, ModuleScope and its ComponentRegistry are available through dependency injection. Page rendering automatically uses that registry, including nested components and layouts. Routes registered directly on the application router have no module scope and use the application registry.

Calling $context->root() changes only where a module registers a route; it preserves the owning module scope. If a shared resource route already exists, its original owner remains unchanged.

Modules can also register services into the application container:

final class BlogToolsModule implements ModuleInterface
{
    public function register(ModuleContext $context): void
    {
        $context->bind(BlogIndexer::class)->toSelf()->singleton();
    }
}

Module bindings are added to the same application container used by routes, pages, command handlers, and components.

If multiple providers register the same token, the last binding wins as long as the token has not already been resolved as a singleton. In practice, modules should avoid surprising collisions, but intentional overrides are allowed:

$context->bind(Mailer::class)->to(FakeMailer::class);