> For the complete documentation index, see [llms.txt](https://quantumphp.gitbook.io/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://quantumphp.gitbook.io/docs/packages/module/usage.md).

# Usage

## Load enabled module routes

Use `ModuleLoader` when you need the route closures for the modules currently enabled in `shared/config/modules.php`.

```php
use Quantum\Module\ModuleLoader;

$loader = new ModuleLoader();
$routes = $loader->loadModulesRoutes();

foreach ($routes as $module => $defineRoutes) {
    $defineRoutes();
}
```

What to expect:

* disabled modules are skipped
* enabled modules without a route file raise an exception during loading
* each module route file returns a closure rather than an array or other value

For development tooling or long-lived processes, use a fresh `ModuleLoader` after editing module config, dependency files, or route files so the next read uses the updated definitions.

## Read module dependencies

```php
use Quantum\Module\ModuleLoader;

$loader = new ModuleLoader();
$dependencies = $loader->loadModulesDependencies();
```

This returns one merged dependency map across enabled modules.

Use `getModuleDependencies('Blog')` when you need the bindings declared by one module.

## Scaffold a new module

```php
use Quantum\Module\ModuleManager;

$manager = new ModuleManager(
    moduleName: 'Blog',
    template: 'DefaultWeb',
    enabled: true,
    withAssets: true,
);

$manager->writeContents();
$manager->addModuleConfig();
```

Recommended order:

1. call `writeContents()` first so the module directory exists
2. call `addModuleConfig()` after files are created

If you call `addModuleConfig()` before the module directory exists, the package raises `missingModuleDirectory()` and leaves `shared/config/modules.php` unchanged.

## Choose templates carefully

Use a template name that exists under `src/Module/Templates`.

Examples available in the package:

* `DefaultApi`
* `DefaultWeb`
* `DemoApi`
* `DemoWeb`
* `Toolkit`

If the template directory is missing, module generation stops before any files are copied.

## Copy assets when the template ships an assets directory

`withAssets` makes the manager copy `Templates/<Template>/assets` into `assets/<ModuleName>`.

That is useful for web-facing templates such as `DefaultWeb`, `DemoWeb`, or `Toolkit`.

When a template has no `assets` directory, leave `withAssets` set to `false`.

## Handle duplicate names and prefixes

`addModuleConfig()` rejects a new module when either of these already exists in `shared/config/modules.php`:

* the same module name
* an existing `prefix` that matches the new module's lowercase name

This prevents two modules from registering the same config key or default prefix.

## Common reminders

* `ModuleLoader` registers dependencies in its constructor, so instantiating it also updates the DI container.
* Disabled modules are skipped for dependency registration.
* `writeContents()` covers filesystem scaffolding, while `addModuleConfig()` updates `shared/config/modules.php`.
* Generated files in a template's `src/` tree receive placeholder replacement, and generated PHP files use the current module base namespace from `request()`.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://quantumphp.gitbook.io/docs/packages/module/usage.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
