stave.dev

Admin Screens

Core ships an Admin module at /admin. It draws a dashboard, holds six screens of its own, and routes /admin/{module}/... to any module that offers an admin controller from its own Admin/ folder.

What the Admin module is

core/modules/Admin/Admin.php is a bundled module with two jobs: the dashboard (Admin::index()) and a router (Admin::__call()). Every other admin screen belongs to the module it administers. A module never depends on Admin; a site without it loses the screens and nothing else.

Needs the database

The admin screens read users, groups, capabilities and the event log. Those tables come from core/sql/core.sql, loaded once into an empty database (core/sql/README.md). stave.dev has no database, so its app/modules.ini turns Admin off and there is no /admin to visit here.

Who can reach it

Every admin controller extends Zero\Core\AdminResponse (core/AdminResponse.php). Its constructor checks Session::can('allow.admin') before anything else, and anyone who fails it — signed in or not — gets the same 404 an unknown URL gets, rendered from a plain Response. There is no login redirect and no access-request page, so nothing reveals that an admin lives at the URL.

LayerWhereEffect
Constructor AdminResponse::__construct() 404 without allow.admin. Inherited by every admin controller, so an offering always needs it.
Class attribute #[RequirePermission('allow.admin')] on Admin and on each of its own screens The ordinary attribute gate.
Your attributes Class or method level on your admin controller Checked before the controller is constructed. This is where a screen asks for something finer than allow.admin.
Card permissions admin-config.php Decides whether the dashboard card shows. It does not gate the URL; the attributes do.

Capabilities such as allow.admin are covered on the permissions page.

The dashboard

The dashboard view writes no link itself. It prints two lists of cards:

A module is listed as an offering when all of these hold:

  1. It is not Admin itself, it has an Admin/ folder, and Modules::enabled() is true for it.
  2. The class Zero\Module\{Module}\Admin\{Module}Admin exists (Zero\Core\Module\... for a bundled module). A bare Admin/ folder is not an offering. A class that throws while loading is logged with Console::warn() and left out; the other cards still show.
  3. The signed-in user holds every key in the card's permissions.

Offering an admin screen

Add an Admin/ folder to your module. The module does not need a public controller.

modules/Links/
├── Links.php               # public controller (optional)
├── view/
└── Admin/
    ├── LinksAdmin.php      # Zero\Module\Links\Admin\LinksAdmin
    ├── admin-config.php    # optional: the dashboard card
    ├── view/
    └── assets/
        ├── css/linksadmin.css
        └── js/linksadmin.js

core/modules/Module/Admin/ is a working example, and core/modules/Module/mkmod scaffolds the folder with --admin.

The controller

The class name and namespace are exact: {Module}Admin in Zero\Module\{Module}\Admin. Beyond that it is an ordinary Zero controller — public methods are endpoints, URL segments arrive as arguments.

<?php
namespace Zero\Module\Links\Admin;

use Zero\Core\AdminResponse;
use Zero\Core\Exception\HTTPError;
use Zero\Core\Attribute\{RequirePermission, AuditLog, AllowedMethods, RequiredParams};

#[RequirePermission('allow.admin')]
class LinksAdmin extends AdminResponse {

    // GET /admin/links
    public function index() {
        $this->build(__DIR__ . '/view/index.php');
    }

    // GET /admin/links/edit/5
    public function edit($id) {
        $this->setBreadcrumbs([
            ['label' => 'Links', 'url' => '/admin/links'],
            ['label' => 'Edit'],
        ]);
        $this->linkId = (int) $id;
        $this->build(__DIR__ . '/view/edit.php');
    }

    // POST /admin/links/save
    #[AuditLog('link_save', includeParams: true)]
    #[AllowedMethods('POST')]
    #[RequiredParams(['url'])]
    public function save() {
        if (!filter_var($_POST['url'], FILTER_VALIDATE_URL)) {
            throw new HTTPError(400, 'That is not a URL');
        }
        // ... store it ...
        $this->done('Link saved', '/admin/links');
    }
}
Inherited from AdminResponseDoes
done(string $message, string $url, array $data = []) Finishes an action that worked. A JSON client gets success, message and $data; a browser is redirected to $url, so a reload does not post the form again.
setBreadcrumbs(array $crumbs) Sets the trail: “Admin”, then your crumbs. The last one is the page itself and is not a link.
addBreadcrumb(string $label, ?string $url = null) Adds one crumb after the default trail (“Admin”, then your card's label).
loadInlineAsset($module, $type, $filename), loadAdminStyles($module), loadAdminScripts($module) Inline an asset from a view (see Assets).

An action that did not work throws an HTTPError: a browser gets the error page with the detail, a JSON client gets it as detail. Breadcrumbs are printed above the view by AdminResponse::beforeView(), which also opens the <div class="admin"> every admin screen sits in; a view prints neither.

The dashboard card

admin-config.php returns the card. Every key is optional, and so is the file.

<?php
return [
    'label'       => 'Manage Links',         // default "{Module} Management"
    'icon'        => 'link',                 // Material Symbol name, default "settings"
    'description' => 'Manage shared links',  // default ""
    'permissions' => ['allow.admin'],        // default ['allow.admin']
];

Whether the module is on, and where its card sits, are not set here. Both live in app/modules.ini (see configuration). Cards are sorted by Modules::order(), modules without an order last, then by name:

[order]
Links = 10
Billing = 20

How /admin/{module}/... is routed

/admin/links/edit/5
→
Admin::__call('links')
→
routeToModuleAdmin()
→
LinksAdmin::edit('5')
  1. Application::run() resolves Admin, checks its class attribute and calls links('edit', '5'), a method Admin does not have.
  2. Admin::__call() turns the segment into a class name (tech-stack → TechStack) and looks for Zero\Module\Links\Admin\LinksAdmin — only when the name is one identifier and Modules::routable() passes, so the admin file of a module that is switched off is never loaded.
  3. AdminResponse::routeToModuleAdmin() converts a kebab-case method to camelCase (create-workflow → createWorkflow), answers 404 unless the method is routable, runs Application::checkAttributes() on your class and method, loads your module's config, constructs the controller and calls the method with the remaining segments.
URLReaches
/adminAdmin::index()
/admin/linksLinksAdmin::index()
/admin/links/edit/5LinksAdmin::edit('5')
/admin/users/edit/5Users::edit('5'), one of Admin's own screens
/admin/nothing404

Which methods a URL can reach

AdminResponse::isRoutableMethod() decides. A method is an endpoint only when it is:

So your protected helpers and everything inherited from the framework answer 404, before the controller is constructed. Make a helper protected or private; a public one is a URL.

Attributes on an admin controller

routeToModuleAdmin() is a dispatcher in its own right, so it calls Application::checkAttributes() for your class and method before constructing the controller. Class-level and method-level attributes behave as they do anywhere else: #[AllowedMethods('POST')] and #[AuditLog] on an action, or a narrower #[RequirePermission] on a class that should not be open to every admin. Admin's own screens put #[AllowedMethods('POST')] and #[AuditLog] on every endpoint that changes something.

Your module's config and strings

The live module of an /admin/... request is Admin, not yours. routeToModuleAdmin() therefore calls Application::loadModuleConfig() for the module whose screen it is, which loads that module's strings/*.ini, its config.ini and every file in its config/. Your admin controller sees the same constants your public controller does.

Assets: inline, never by URL

Nothing under an Admin/assets/ folder is reachable from the web root. AdminResponse enforces that in two places:

The price is no browser caching between admin pages. Ordinary module assets work differently; see assets.

Files loaded by name

When an admin page is built through the frame, two folders are searched in order: core/modules/Admin/assets/{css|js}/, then assets/{css|js}/ beside your controller. Only these filenames load, and a name found in both loads once, from the first:

FileLoads on
admin.css, admin.jsEvery admin page. The shared base that ships with Admin.
{class}.css, {class}.jsEvery page of your controller. The class name in lowercase: linksadmin.css for LinksAdmin.
{endpoint}.css, {endpoint}.jsFor an offering the endpoint is the module segment of the URL, so links.css loads on every page of /admin/links/... and edit.css does not load.

Files loaded from a view

For a file that belongs to one page, call a loader from the view. These read {Module}/Admin/assets/ directly, not its css/ and js/ subfolders. core/modules/Entity/Admin/view/create.php does exactly this:

<?php $this->loadInlineAsset("Entity", "css", "create.css"); ?>

<!-- the view -->

<?php $this->loadInlineAsset("Entity", "js", "create.js"); ?>

loadAdminStyles($module) and loadAdminScripts($module) inline every *.css or *.js directly in that folder. A missing file is logged with Console::warn(). The file is included, so PHP tags in it run.

Look

admin.css scopes its rules to .admin and takes its colours from --admin-* tokens that read your site theme's variables, with fallbacks. Build a view from the shared classes (admin-head, admin-panel, admin-table, btn-primary, …) and use the tokens rather than naming colours. core/docs/ADMIN_ASSETS.md lists both.

Admin's own screens

Six screens ship in core/modules/Admin/submodule/. Each is a submodule of Admin that extends AdminResponse, in the namespace Zero\Core\Module\Admin, with its own views, assets and card.

ScreenURLWhat it does
Users /admin/users Lists accounts; edit/{id} shows one user's capabilities and settings. Grants or revokes a capability, writes or removes a setting, disables or enables the account.
Groups /admin/groups Lists groups; creates, edits and deletes them; adds and removes members and sets a member's role.
Permissions /admin/permissions Lists every capability key with who holds it, and grants or revokes one for several users and groups at once. Users answers “what may this user do”; this answers “who may do this”.
Modules /admin/modules Lists every module with its tier, whether it is on, whether a site module overrides core's, and its card order. POST /admin/modules/set changes one by rewriting app/modules.ini.
Audit Log /admin/audit Every recorded event, newest first, with filters: what #[AuditLog] wrote plus the framework's own events.
Firewall /admin/firewall Lists the IP addresses blocked at Cloudflare and unblocks one.

Their cards are ordered by an order key in each screen's own admin-config.php, not by app/modules.ini.

Admin's own names win

A module called Users cannot take over /admin/users. Admin::__call() checks its own screens first, and logs a warning that your offering is unreachable. Avoid naming a module Users, Groups, Permissions, Modules, Audit or Firewall if it needs an admin screen.

When a card is missing