Module admin
How a module adds a sidebar row to the admin panel, its own screens, actions on them and modal windows.
By the end your module has a row in the admin sidebar with tabs for its screens, a screen with a list of records, and an action on a table row. The core draws all of it: the module declares the screen schema and the handlers, with no HTML or JavaScript of its own.
Minimal example
The Example module lists player reports and lets staff close them. The admin.example and admin.example.view permissions are declared as in module permissions.
<?php
declare(strict_types = 1);
namespace Flute\Modules\Example\Admin\Nav;
use Flute\Core\Players\Contracts\AdminNavProvider;
use Flute\Core\Players\Values\NavContribution;
final class ExampleNav implements AdminNavProvider
{
public const REPORTS = '/admin/example';
public const ARCHIVE = '/admin/example/archive';
public function navItems(): array
{
return [
new NavContribution(
group: 'community',
key: 'example',
label: 'example-admin.nav.reports',
icon: 'sl.linear.flag',
href: self::REPORTS,
permission: 'admin.example.view',
),
new NavContribution(
group: 'community',
key: 'example-archive',
label: 'example-admin.nav.archive',
icon: 'sl.linear.archive',
href: self::ARCHIVE,
permission: 'admin.example.view',
order: 110,
),
];
}
}<?php
declare(strict_types = 1);
namespace Flute\Modules\Example\Admin;
use Flute\Admin\Support\AbstractAdminPackage;
use Flute\Core\Surfaces\SurfaceKeys;
use Flute\Core\Surfaces\SurfaceRegistry;
use Flute\Modules\Example\Admin\Nav\ExampleNav;
final class ExampleAdminPackage extends AbstractAdminPackage
{
public function initialize(): void
{
parent::initialize();
$this->loadRoutesFromFile('routes.php');
app(SurfaceRegistry::class)->add(SurfaceKeys::ADMIN_NAV, ExampleNav::class);
}
}<?php
declare(strict_types = 1);
use Flute\Core\Router\Router;
use Flute\Modules\Example\Admin\Nav\ExampleNav;
use Flute\Modules\Example\Admin\Pages\ArchivePage;
use Flute\Modules\Example\Admin\Pages\ReportListPage;
Router::page(ExampleNav::REPORTS, ReportListPage::class);
Router::page(ExampleNav::ARCHIVE, ArchivePage::class);<?php
declare(strict_types = 1);
namespace Flute\Modules\Example\Admin\Pages;
use Flute\Admin\UI\Action\ActionDescriptor;
use Flute\Admin\UI\Action\ActionRegistry;
use Flute\Admin\UI\Page;
use Flute\Admin\UI\PageContext;
use Flute\Admin\UI\Schema\Component;
use Flute\Admin\UI\Schema\Schema;
use Flute\Admin\UI\Table\Column;
use Flute\Admin\UI\Table\RowAction;
use Flute\Admin\UI\Table\Table;
use Flute\Modules\Example\Admin\Handlers\CloseReport;
use Flute\Modules\Example\database\Entities\Report;
final class ReportListPage extends Page
{
public const TABLE = 'example-reports';
public function title(): string
{
return 'example-admin.nav.reports';
}
public function permission(): string
{
return 'admin.example.view';
}
public function schema(PageContext $ctx): Schema
{
$rows = array_map(static fn(Report $r): array => [
'id' => $r->id,
'subject' => $r->subject,
], Report::query()->where('closedAt', null)->orderBy('id', 'DESC')->fetchAll());
return Schema::make([
Component::make('page-header', 'example-header')->props(['title' => 'example-admin.nav.reports']),
Table::make(self::TABLE)
->columns([Column::make('subject', 'example-admin.reports.subject')])
->rowActions($ctx->can('admin.example') ? [
RowAction::make('close', 'example-admin.reports.close')
->icon('sl.linear.check-circle')
->requiresConfirmation()
->confirmText('example-admin.reports.close_confirm'),
] : [])
->rows($rows)
->total(count($rows)),
]);
}
public function actions(PageContext $ctx): ActionRegistry
{
return (new ActionRegistry())->register(
ActionDescriptor::make('close')
->authorize('admin.example')
->authorizeRecord(
static fn(PageContext $c): ?Report => Report::findByPK((int) $c->input('id')),
'example.reports.close',
'admin.example',
)
->handler(CloseReport::class),
);
}
}<?php
declare(strict_types = 1);
namespace Flute\Modules\Example\Admin\Handlers;
use Flute\Admin\UI\Action\ActionResult;
use Flute\Admin\UI\PageContext;
use Flute\Modules\Example\Admin\Pages\ReportListPage;
use Flute\Modules\Example\Services\Reports;
final class CloseReport
{
public function __construct(
private readonly Reports $reports,
) {
}
public function __invoke(PageContext $ctx): ActionResult
{
$this->reports->close((int) $ctx->input('id'));
return ActionResult::make()
->toast(__('example-admin.reports.closed'))
->reload(ReportListPage::TABLE);
}
}The package is loaded from the module provider: the provider implements Flute\Core\Contracts\RequestBootableInterface, and its bootRequest() holds if (is_admin_path()) { $this->loadPackage(new ExampleAdminPackage()); }.
How it works
loadPackage() works only on /admin addresses and only for someone holding the admin permission. The core calls the package's initialize(), which registers the routes from routes.php (the path is relative to the package class's folder) and the menu provider. Router::page() puts one handler on the address: GET draws the screen, GET with ?_fragment= redraws one part, POST with ?_action=<name> runs an action.
All NavContributions of one module fold into one sidebar row, in the group of the first one. The row leads to the first one's address and carries its label, or the module name (the <module key>.title key) when there are several. Each contribution becomes a tab on every screen of the module, with a "Settings" tab last if the module has settings. A tab is shown when the admin holds its permission.
Before showing a screen the core calls the page's authorize(), which by default checks permission(). Before an action it checks the descriptor's ->authorize() and ->authorizeRecord(): the record's policy if there is one, otherwise the fallback permission. A table row button posts the action name and the row's id.
Modal windows
A short form or a confirmation opens in a modal window. It is a class implementing Flute\Admin\UI\Surface\Contracts\AdminSurface, registered in the package's initialize(): app(AdminSurfaceRegistry::class)->register(new ReportSurface()). The window body is fetched on open from /admin/ui/surface?_surface=<id>&id=<n>; it is never embedded in the page schema.
| Method | What it does |
|---|---|
id(): string | window name, for example example.report |
authorize(PageContext $context): bool | whether the window may open |
resolveRecord(PageContext $context): ?object | the record for the id in the address. null gives 404 |
recordAbility(): ?string | the ability the record is checked against before rendering |
recordFallbackPermission(): ?string | the permission used when the record has no policy |
actions(PageContext $context): ActionRegistry | actions of the window's forms |
render(PageContext $context): ?string | body HTML. null gives 404 |
Open the window with a button, Component::make('button', ...)->props(['surface' => 'example.report']), with a table row, ->rowSurface('example.report'), or from an action: ActionResult::make()->openSurface('example.report', ['type' => 'modal', 'record' => $id]).
Reference
NavContribution(string $group, string $key, string $label, string $icon, string $href, string $permission = '', int $order = 100, bool $advanced = false). Groups: content ("Site"), community ("Players"), servers, monetization ("Store"), activities ("Activities"), settings, maintenance, developer. An unknown group becomes a section of its own.
| Class, method | What it does |
|---|---|
Page::schema(PageContext $ctx): Schema | required, what the screen is made of. The old Platform\Screen and Router::screen() are frozen |
Page::title(): ?string, permission(): string|array|null | screen title and permission. null opens the screen to any admin |
Page::actions(PageContext $ctx): ActionRegistry | the screen's named actions. A name missing from the registry never runs |
ActionDescriptor::make(string $name) | ->authorize(string|array), ->authorizeRecord(callable, string $ability, ?string $fallback), ->handler(callable|class-string), ->requiresElevation() for money, bans and deletion |
ActionResult::make() | ->toast($message, $type), ->reload($componentId), ->openSurface(), ->closeSurface(), ->redirect($url), ->validationErrors([...]) |
Common mistakes
Permission denied
The screen or action answered 403: the admin lacks the permission from permission() or ->authorize(). A permission you just declared is in the registry, but no role holds it yet. Grant it to a role, run php flute cache:clear and sign in again.
The screen answers as if it did not exist. The package did not load: the account lacks admin, or loadPackage() is called in boot(). In worker mode boot() runs once per process while the package depends on the request's address and user, so load it in bootRequest().
An action without ->authorize() runs for anyone who opened the screen. Give a mutating action the write permission admin.example: everyone who sees the screen already holds admin and admin.example.view.
In routes.php static addresses go before addresses with {id}: /admin/example/create before /admin/example/{id}.