Module provider
What goes into register(), boot() and bootRequest(), the order in which the core loads modules, and how a module copes without an optional neighbour.
By the end your module has a provider that registers its services, loads the module folders and keeps the site working when an optional neighbouring module is missing.
Minimal example
The provider is listed in module.json. Tickets is declared as an optional neighbour: when it is installed, the core loads it before Example.
{
"name": "Example",
"version": "1.0.0",
"providers": ["Flute\\Modules\\Example\\Providers\\ExampleProvider"],
"dependencies": {
"flute": "^2.0",
"suggests": { "Tickets": ">=3.0.0" }
}
}<?php
declare(strict_types = 1);
namespace Flute\Modules\Example\Providers;
use DI\Container;
use Flute\Core\Contracts\RequestBootableInterface;
use Flute\Core\Support\ModuleServiceProvider;
use Flute\Modules\Example\Admin\ExampleAdminPackage;
use Flute\Modules\Example\Contracts\PayoutSender;
use Flute\Modules\Example\Services\LogPayoutSender;
final class ExampleProvider extends ModuleServiceProvider implements RequestBootableInterface
{
public function register(Container $container): void
{
$container->set(PayoutSender::class, \DI\get(LogPayoutSender::class));
}
public function boot(Container $container): void
{
$this->bootstrapModule();
$this->loadViews('Resources/views', 'example');
}
public function bootRequest(Container $container): void
{
if (is_admin_path()) {
$this->loadPackage(new ExampleAdminPackage());
}
}
}Services take their dependencies through the constructor. You do not register the class itself: PHP-DI builds Payouts on its own and passes in PayoutSender from the binding made in register(). A binding is only needed where the type is an interface. The BrowserPush module binds its PushSender to an implementation the same way.
<?php
declare(strict_types = 1);
namespace Flute\Modules\Example\Services;
use Flute\Core\Database\Entities\User;
use Flute\Modules\Example\Contracts\PayoutSender;
final class Payouts
{
public function __construct(
private readonly PayoutSender $sender,
) {
}
public function pay(User $user, int $amount): void
{
$this->sender->send($user, $amount);
}
}The provider itself is created with a bare new, so it has no constructor with dependencies. Inside boot() take services from $container; controller action arguments and console commands are built by the container.
How the core loads modules
Modules are loaded by the core's ModulesServiceProvider, which comes in the list after payments, servers and updates. Then, step by step:
ModuleFinderreadsapp/Modules/*/module.json, and when there is none, looks exactly one folder deeper. Folders whose name starts with a dot are skipped.ModuleManagerreads the statuses from the database, andModuleDependencieschecks every active module. A module with an unmet dependency is switched off before it loads.- Providers are sorted: modules from
dependencies.modulesanddependencies.suggestsfirst, then by theorderfield. ModuleRegistercallsregister()on every active module and only thenboot()on each. Soboot()can already use a binding from a module that comes later in the list.- On every HTTP request, after the same pass over the core,
bootRequest()is called on providers that implementRequestBootableInterface.
Core providers have contexts(), which limits them to the site, the admin panel, the API or the console. A module provider has no such method, and boot() runs everywhere: on site pages, in /admin, in /api, in php flute cron:run and in the queue worker. That is why boot() only writes class names into registries, and heavy or request-dependent work moves to bootRequest().
In worker mode boot() runs once per process and bootRequest() before each request. Listeners and routes added in bootRequest() are removed after the request. A stored anonymous page from the cache is sent before modules load, and then neither boot() nor bootRequest() runs. What else must not survive between requests is covered in Worker mode.
| Method | When | What belongs here |
|---|---|---|
register(Container $container): void | once, before any boot() | only $container->set() bindings: other modules have not registered yet |
boot(Container $container): void | on every request and in the console; once per process in worker mode | bootstrapModule(), loadViews(), event listeners, registry entries, scheduled tasks, console commands behind is_cli() |
bootRequest(Container $container): void | on every HTTP request, never in the console | the admin package, anything reading user() or request(), anything that depends on the visitor |
Another module: check for it and do without it
module_active('Tickets') tells you whether a module is switched on. class_exists() alone is not enough: Composer autoloads a module's classes even when the module is off. Declare the neighbour in dependencies.suggests so it loads first, and subscribe to it only after the check:
use Flute\Modules\Example\Services\StaffRewards;
use Flute\Modules\Tickets\Events\TicketReplied;
public function boot(Container $container): void
{
$this->bootstrapModule();
if (module_active('Tickets') && class_exists(TicketReplied::class)) {
events()->addListener(
TicketReplied::NAME,
static fn(TicketReplied $e) => $e->isStaff ? app(StaffRewards::class)->replied($e->ticketId) : null,
);
}
}The Discord module does this: it listens to replies in requests only while Tickets is on. Without Tickets the rest of Discord works as usual.
When you need the module record, use ModuleManager. getModule() throws on an unknown key, so call issetModule() first:
$modules = app(ModuleManager::class);
$isActive = $modules->issetModule('Tickets')
&& $modules->getModule('Tickets')->status === ModuleManager::ACTIVE;Reach the neighbour only through its Services, Contracts and events. Leave its controllers, entities and Support alone.
Common mistakes
The module silently does nothing
The core writes an error from register() or boot() to the modules log and marks the module as broken, and the site keeps running without it. The exception shows on the page only in debug mode and never in the console. Switch to development mode and look in storage/logs.
| What you see | Cause |
|---|---|
ArgumentCountError in the modules log | the provider has a constructor with arguments |
... must extend Flute\Core\Support\ModuleServiceProvider | the provider does not extend the base class |
Module ... wasn't found in the FileSystem! | the class in providers was not found: a typo, or the namespace does not match the path |
[EMERGENCY MODULE SHUTDOWN] Flute module "Example" dependency check failed - ... | a requirement from dependencies is not met, the module was switched off |
Module dependency cycle detected: Example -> Tickets -> Example | modules depend on each other in a circle; in debug mode loading fails |
[MODULE ISOLATION] Module "Example" attempted to override protected core binding "...". Modules must not rebind core services. | the module redefined a protected core service; the list is in config/modules.php under protected_bindings |
Assets file does not exist: ... | loadScss() got a path to a file that does not exist |
Module Tickets wasn't found | getModule() called without issetModule() |
| a command or task is missing in the console | it is registered in bootRequest(), which the console never calls |
a listener from protected array $listen never fires | a module provider's property is not read; subscribe in boot() |
| Method | What it does |
|---|---|
bootstrapModule(): void | loads the module folders present on disk: database/Entities, Resources/config, Resources/lang, Resources/pages, Controllers or Http/Controllers, Components, Widgets, Surfaces |
loadViews(string $viewDirectory, string $namespace): void | a Blade namespace, then view('example::index') |
loadScss(string $assetsFile): void | module styles, sent only on pages where one of its views rendered |
loadPackage(AdminPackageInterface $package): void | the admin package; does nothing outside /admin or without the admin permission |
registerNotificationTemplates(NotificationTemplateProviderInterface $provider): void | notification templates |
setUpdateChannel(string $org, string $rep): self | updates from GitHub Releases |
setCacheDuration(int $seconds): self | module cache lifetime, 3600 by default |
clearFileCache(): void | drops the cache tagged module.<Name> |
isFrontendHtmlRequest(): bool | a site page request, not /admin, /api, /wire or the console |
isApiLikeRequest(): bool | a request under /api |
isInfrastructureCliCommand(): bool | the running command is route:list, cache:clear, cache:warmup or cache:prune |
An entry in providers is a class string or an object {"class": "...", "order": 10}. After the dependency sort, providers are ordered by order ascending; without it order is 0. Without a declared dependency the order of two modules is not guaranteed.
| Key | Example | Message when unmet |
|---|---|---|
flute | "^2.0" | Flute version ^2.0 or higher is required. |
php | ">=8.2" | PHP version >=8.2 or higher is required. |
extensions | ["gmp"] | PHP extension gmp is required. |
composer | {"guzzlehttp/guzzle": "^7.0"} | Composer package guzzlehttp/guzzle version ^7.0 or higher is required. |
modules | {"Tickets": ">=3.0.0"} | Module Tickets version >=3.0.0 or higher is required. |
theme | {"standard": ">=2.0"} | Theme standard version >=2.0 or higher is required. |
suggests | {"Tickets": ">=3.0.0"} | never checked, only loads that module first |
A module that an active module depends on cannot be switched off or deleted.