Skip to Content
Module development

Modules

How a Flute 2.0 module is built: module.json, provider, routes, database, extension points and packaging.

This page walks through how a Flute 2.0 module is built, from module.json to an archive for the marketplace, using a MyFeature module (a frag leaderboard) as the example. There is no generator, you write the module by hand, but two files are enough to load it: module.json and a provider.

A module lives in the app/Modules/<Name>/ folder. The core finds it, reads the manifest, checks dependencies, applies migrations and calls register() on the provider, then boot(). The module talks to the core only through extension points.

Porting a module from Flute 1.x? Read 1.x modules on Flute 2.0 first: it lists what will break. The old API is described in the 1.x documentation.

Structure

app/Modules/MyFeature/
├── module.json                  name, version, providers, dependencies, permissions
├── composer.json                own composer packages, optional
├── Installer.php                one-off steps on install and uninstall, optional
├── Updater.php                  data migration between versions, optional
├── Providers/
│   └── MyFeatureProvider.php    entry point, listed in module.json
├── Controllers/                 controllers with #[Route]           (auto-scanned)
├── Services/                    module logic and its public API
├── database/
│   ├── Entities/                Cycle ActiveRecord entities         (auto-scanned)
│   └── migrations/              migrations, only via generate:migration
├── Admin/                       admin package and its pages
├── Surfaces/                    embedding into cards and profiles   (auto-scanned)
└── Resources/
    ├── views/                   Blade
    ├── pages/                   Blade files that become GET pages   (auto-scanned)
    ├── lang/{ru,en}/domain.php  translations                        (auto-scanned)
    ├── config/myfeature.php     default settings                    (auto-scanned)
    └── assets/{scss,js}/        styles and scripts

"Auto-scanned" means bootstrapModule() loads the folder if it exists.

You added a folder and it does not show up

Outside debug mode the folder list is cached. Run php flute cache:clear.

module.json

app/Modules/MyFeature/module.json
{
    "name": "MyFeature",
    "version": "1.0.0",
    "description": "Leaderboard of frags across all servers",
    "authors": ["Your Name"],
    "providers": ["Flute\\Modules\\MyFeature\\Providers\\MyFeatureProvider"],
    "dependencies": {
        "flute": "^2.0"
    },
    "permissions": [
        { "name": "myfeature.view", "group": "myfeature" },
        { "name": "myfeature.manage", "group": "myfeature", "implies": ["myfeature.view"] }
    ]
}

The module key is the folder name, not name. The namespace mirrors the path: app/Modules/MyFeature/ → Flute\Modules\MyFeature\.

In dependencies, the checked keys are flute, php, extensions, composer, modules and theme. Version constraints are written as in Composer: ^2.0, >=2.1. A bare version 2.0.0 is read as >=2.0.0. Optional modules are listed in dependencies.suggests: their presence is not checked, but if they are there, they load first.

Provider

The provider extends Flute\Core\Support\ModuleServiceProvider. The core creates it with new and no arguments, so the constructor must have no dependencies. Call order: register($container), then boot($container).

Providers/MyFeatureProvider.php
<?php

namespace Flute\Modules\MyFeature\Providers;

use DI\Container;
use Flute\Core\Contracts\RequestBootableInterface;
use Flute\Core\Support\ModuleServiceProvider;
use Flute\Modules\MyFeature\Services\LeaderboardService;

class MyFeatureProvider extends ModuleServiceProvider implements RequestBootableInterface
{
    public function register(Container $container): void
    {
        $container->set(LeaderboardService::class, \DI\autowire());
    }

    public function boot(Container $container): void
    {
        $this->bootstrapModule();
        $this->loadViews('Resources/views', 'myfeature');
    }

    public function bootRequest(Container $container): void
    {
        $this->loadScss('Resources/assets/scss/myfeature.scss');
    }
}

Start boot() with bootstrapModule(), which loads all auto-scanned folders. Move anything heavy or request-dependent to bootRequest() (the RequestBootableInterface interface): boot() runs on every request, the console and the API included. Subscribe to events in boot() with events()->addListener(...), the protected array $listen property on a module is not read.

The module silently does nothing

The core catches an exception inside boot() and writes it to the modules log, and a live site shows nothing. Debug with APP_ENV in development mode.

Every provider method and the load order are described in Module provider.

Routes

Routes are declared with an attribute on a controller method; there are no route files. The Controllers/ folder is auto-scanned.

Controllers/MyFeatureController.php
<?php

namespace Flute\Modules\MyFeature\Controllers;

use Flute\Core\Router\Annotations\Route;
use Flute\Core\Support\BaseController;
use Flute\Core\Support\FluteRequest;
use Flute\Modules\MyFeature\Services\LeaderboardService;

class MyFeatureController extends BaseController
{
    #[Route(uri: '/frags', methods: ['GET'], name: 'frags.index')]
    public function index(FluteRequest $request, LeaderboardService $leaderboard)
    {
        return view('myfeature::index', [
            'rows' => $leaderboard->top(50),
        ]);
    }

    #[Route(uri: '/frags/claim', methods: ['POST'], name: 'frags.claim', middleware: ['auth', 'csrf'])]
    public function claim(FluteRequest $request)
    {
        // ...
    }
}

Action parameters are resolved from the container. A page without a controller: put a Blade file into Resources/pages/ and it becomes a GET route. Resources/pages/sub/x.blade.php answers at /sub/x, without a module prefix.

Core middleware, CSRF and REST API endpoints are covered in Module routes, middleware and API.

Database

Entities live in database/Entities/ with the namespace Flute\Modules\<Name>\database\Entities: the database segment is lowercase.

Migrations live only in the module's database/migrations/ and are created with a command. It asks what the migration is for: the core or a specific module.

php flute generate:migration
php flute migrate

On install the core applies the module's migrations and creates missing entity tables and columns. If something fails, the whole migrations folder is rolled back. When the module is removed, its migrations are rolled back in full, from newest to oldest.

Do not create tables when the module boots and do not put its migrations into storage/migrations: they will not be rolled back on removal. Do not change core tables destructively.

Entities, migrations and the schema changes the core makes are described in Module tables and migrations.

Admin panel and permissions

Permissions are declared in module.json.permissions[]. On install the core saves them to the permission registry. A permission with the same name as a core permission is rejected. Every admin page and action checks permissions: can_read(), can_write() or middleware: ['can:...'].

The admin package extends Flute\Admin\Support\AbstractAdminPackage and is wired in with loadPackage(). Pages are Flute\Admin\UI\Page classes, registered with Router::page() in the package's routes.php. This is the only route file allowed in a module.

Declaring permissions is covered in Module permissions, package screens in Module admin.

Extension points

The full catalog is in app/Core/AGENTS.md, section "Extension points". Common tasks:

You needWhat to use
register a service$container->set(Foo::class, \DI\autowire()) in register()
react to a core eventevents()->addListener(Event::NAME, fn ($e) => ...) in boot()
own pages and API#[Route] in Controllers/, plus ApiRegistry and ScopeRegistry for the API
record-level permissionsPolicyRegistry
a profile tab, a row or an action in the user cardclasses in Surfaces/
a block the owner places on a page in Studioa Section subclass with an owner.name key and ModuleBlocks::register()
an interactive componentWireSurface, registered with WireRegistry->lazy()
a command on the game serverServers\GameActions::send()
a badge, line, tab or action on a playerPlayerHeaderBadge, PlayerCardLine, PlayerTabProvider, PlayerQuickAction
charge or refund moneyPayments\Services\Purchase, a gateway through GatewayDriver
moderation queue, reports, commentsModeration\Contracts\InboxSource, ReportableSubject, CommentableSubject
a console commandapp()->getConsole()->addCommand(...) when PHP_SAPI === 'cli'
a scheduled taskapp(Scheduler\TaskScheduler::class)->add('vendor.task', fn () => ...)->daily() in boot()
global search resultsGlobalSearch\Services\SearchRegistry
sitemap entriesSitemapProviderRegistry
own settings in the admin panela class in Surfaces/ with #[AdminSettings] that implements SettingsProviderInterface

If there is no suitable point, branching on the module name is not added to the core. Instead, an interface is declared in app/Core/Contracts/, the core gets a default implementation, and the module substitutes its own.

All extension points with examples are in Extension points, core events in Events and listeners.

Other modules

A module talks to another one only through its public Services and Contracts, declares the dependency in module.json and does not break the page when the other module is missing.

use Flute\Core\ModulesManager\ModuleManager;

private function exampleAvailable(): bool
{
    $manager = app(ModuleManager::class);

    return $manager->issetModule('Example')
        && $manager->getModule('Example')->status === ModuleManager::ACTIVE;
}

getModule() throws an exception on an unknown key, so call issetModule() first. A module that an active module depends on cannot be disabled or removed.

Translations, settings, cache

Translations live in Resources/lang/<lang>/<domain>.php, the domain is the file name. It helps to keep admin strings in a separate domain, for example myfeature-admin.php. You need at least ru and en.

Default settings live in Resources/config/myfeature.php and are read with config('myfeature.key', $default). This file is never overwritten: what the owner saves in the admin panel goes on top, into config/myfeature.php. Describe the fields with #[AdminSettings], and the core renders and saves the settings page itself.

Name cache keys module.<Name>.<what> and write them with cache()->callback($key, $cb, $ttl, $tags). To clear the module's cache from inside, use clearFileCache(), from outside, php flute cache:clear.

Each topic has its own page: Module translations, Module settings and Cache and worker mode in a module.

Worker mode

On FrankenPHP one process serves many requests in a row. Anything a module keeps in static or in a service built in boot() outlives the request and reaches the next visitor. The result: the second visitor sees the first one's name.

So do request-dependent registration in bootRequest(). Cache a value the page reads several times with Support\RequestMemo::remember(), and call forget() where you change it.

How to check

Run php flute serve --worker --workers=1 and visit the site as two users one after the other. Each must see their own data.

What else breaks in worker mode is described in Cache and worker mode in a module.

Packaging

A module is distributed as a zip archive of up to 100 MB unpacked. module.json sits at the archive root or exactly one folder deeper, and paths with ../ are rejected. List your own composer packages in app/Modules/<Name>/composer.json: the root composer.json picks them up and ignores scripts and extra from the module.

At runtime the module writes nothing into its own folder: an update replaces the folder entirely. Keep the module's files in storage/.

Do not rename the folder

The folder name is the module key and a namespace segment. Rename it after a release, and the module breaks for everyone who already installed it.

Before release

  • module.json is valid: name, version, providers, dependencies.flute
  • the namespace mirrors the path
  • the provider is created without arguments, boot() starts with bootstrapModule()
  • no use of other modules' internals, every dependency is declared in module.json
  • the page does not break when an optional module is missing (checked by disabling it)
  • every entity has a migration, install and removal pass on a clean database
  • removal leaves no tables, files or menu items
  • every admin page and action checks permissions, and the permissions are declared in module.json
  • ru and en translations exist, the code has no interface strings
  • the interface is built from <x-*> components, sl.linear.* icons, no inline SVG
  • vendor/bin/phpstan analyse -c phpstan-modules.neon.dist app/Modules/<Name> reports no errors
  • checked in a browser after php flute cache:clear
  • in worker mode two users in a row each see their own data
  • Updater migrates data from the previous published version

More in the engine repository

  • docs/module-guide.md: a module from scratch, the contracts the core requires from a module (§14), rules for client scripts (§15)
  • app/Core/AGENTS.md: the extension point catalog
  • docs/ui-playbook/: how to design module screens

On this page