stave.dev

Bundled Modules

Nine modules come with core, in core/modules/. Six are on by default, three are examples that start off. A site can turn any of them off, or override one with a module of the same name.

The Nine Modules

A bundled module follows the same URL rules as any other. Its namespace is what sets it apart: every class of a module in core/modules/ is in Zero\Core\Module\ (Zero\Core\Module\Auth, Zero\Core\Module\Push\Lib\PushHelper), and Zero\Module\ holds the site's modules only. Every other module lives in the site's modules/.

Module Tier What it is
Index core The landing page and the home of standalone pages. Core's is an empty class with two views: / and /privacy
Auth core Signing in and out: OAuth with Google, GitHub, Discord and GitLab (/auth/user, /auth/complete), passwordless registration and one-time sign-in links by email (/auth/register, /auth/signin, /auth/token), connecting further providers to an account, /auth/logout. Its submodule AccessRequest lets a signed-in user ask for a permission they lack: a form at /auth/access-request/permission?p={key}, a mail to NOTIFY_EMAIL, and an approval link
User core The signed-in user's own pages: the sign-in page /user/login, /user/profile, their permissions, and their API tokens (/user/tokens, create, delete)
Admin core The /admin dashboard and router, behind allow.admin. Its own screens are Users, Groups, Permissions, Modules, Firewall and Audit; every other admin screen is offered by the module it administers (Admin)
Push core Browser push notifications: the /push/* endpoints the browser calls, PushHelper for sending from code, and the /admin/push screen (see Push)
Entity core Admin screens for the Entity Framework's data at /admin/entity. It has no public controller, so there is no /entity page
Module example The reference module: a worked example of every directory a module can have, and the template mkmod copies from (Modules)
HelloWorld example A small module with four endpoints showing views, JSON responses and attribute gates (walkthrough)
Test example Diagnostic endpoints that dump the request, session, server variables and constants. Leave it off on a public site

Tiers and Defaults

Zero\Core\Modules (core/Modules.php) answers every question about where a module is and whether it is on. The tiers are constants in that class:

Tier Modules Default
coreIndex, Auth, User, Admin, Push, Entityon
siteevery module not named hereon
exampleModule, HelloWorld, Testoff

app/modules.ini

The file lists only the departures from those defaults, in two sections:

; Only departures from the defaults.
[enabled]
Push = off
HelloWorld = on

[order]
Links = 10

This site's own file is a real example. stave.dev has no database and no sign-in, so it turns off every module of core that needs one. /user/login and /admin are 404s here. Index needs no entry: the site's own modules/Index/ overrides core's.

[enabled]
Auth = off
User = off
Admin = off
Push = off
Entity = off

A Module That Is Off

A module that is off is treated at every step exactly as a module that does not exist:

Step What happens
Routing The controller is not loaded (Application::controllerFile()), and the view fallback in Response finds no view in its view/
Configuration Its config.ini, config/ and strings/ are not read
Frame Its frame/ and aux/ are not used
Admin /admin/{module} answers 404. The check comes before the admin class is looked up, so the admin file is never loaded
Submodules Unreachable, because the parent is
Assets Module::linkAssets() creates no public link for it, and every existing link into its folder under assets/ is removed

Asset-Link Cleanup

The removal does not run on every request. Application::run() compares a hash of the disabled modules' real paths with storage/cache/modules-assets.marker and cleans up only when they differ: a module was switched off, or a new example-tier folder appeared.

Overriding a Bundled Module

Application::registerAutoloaders() hands Modules two roots in priority order: the site's modules/ first, core/modules/ second. A directory in a root is a module when its name matches ^[A-Z][A-Za-z0-9]*$.

A site overrides any module of core by having a module of the same exact name in its modules/:

<?php
namespace Zero\Module;

// The site's Auth answers /auth; core's class is still there to extend.
class Auth extends \Zero\Core\Module\Auth
{
}

mkmod refuses a bundled module's name, so an override is written by hand.

Index

Core ships core/modules/Index/, an empty class with two views, so a site works with an empty modules/. A site that has modules/Index/ overrides it like any other module. The sign-in page links to /privacy, so the site's Index carries its own view/privacy.php.

Name Clashes

Two things are a configuration error and not a missing page: two spellings of one name (Links and links, compared ignoring case and hyphens), and one name in two folders of the site. The exact name of a module of core in the site's modules/ is an override, not a clash. Application::run() checks at the start of every request (Modules::assertNoClash()):

Switching Modules From the Admin Screen

/admin/modules lists every module of both roots with its tier, where it lives (core or site), whether it is on, whether that is the default, and its order. A module of core the site overrides is marked "Overridden". Admin's own row says how to turn Admin back on (delete the Admin = off line from app/modules.ini), because turning it off removes the screen, and the browser asks before saving that change.

Zero\Core\ModulesIni (core/ModulesIni.php) is the only writer of app/modules.ini. It:

Permissions

The web server user must be able to write the folder that holds app/modules.ini: the temp file, the lock file and the rename need it. An existing file keeps its permission bits; a file with no write bit at all is treated as deliberately read-only and refused. zero-init.sh puts modules.ini.lock and .modules.ini.*.tmp in the site's app/.gitignore; modules.ini itself is committed.

The rest of the admin screens are on Admin.

Push

Browser push notifications over the Web Push API with VAPID keys, sent from the server with no third-party service. The module is on by default but does nothing until the site supplies what core/docs/PUSH_NOTIFICATIONS.md lists:

A site needs Detail
minishlink/web-push ^8.0 Installed with Composer in the site (Getting Started). It signs with the VAPID keys and encrypts the payload
app/config/vapid.ini VAPID_PUBLIC_KEY and VAPID_PRIVATE_KEY. PushHelper and /push/vapid-key read the file directly
PRIMARY_DOMAIN The VAPID subject is https:// followed by it
HTTPS Web Push does not work without it
The push_* tables push_subscriptions, push_notifications, push_notification_clicks, in core/sql/core.sql
Three frontend files of its own A service worker served as /push-worker.js, a script that registers it and subscribes the browser, and a toggle in the frame. Core does not ship them

Sending from a module goes through the Push module's lib class:

use \Zero\Core\Module\Push\Lib\PushHelper;

$notificationId = PushHelper::createNotification([
    'title' => 'New Message',
    'body' => 'You have a new message from John',
    'url' => '/messages/123',                   // optional, / when absent
    'created_by' => \Zero\Core\Session::userId()
]);

$result = PushHelper::sendPush($notificationId, [456]);   // these users, every device of each
$result = PushHelper::sendPushToAll($notificationId);     // every subscription
// $result: ['success' => int, 'failed' => int], counted per device
Endpoint Login Does
POST /push/subscriberequiredSaves the browser's subscription
POST /push/unsubscriberequiredRemoves it
GET /push/statusrequiredsubscribed and count for the signed-in user
POST /push/clickrequiredRecords a click
GET /push/vapid-keypublicThe public key, for the browser to subscribe with

The acting user is Session::userId() and nothing else: a user_id in the request is ignored (core/modules/Push/Push.php). A site that does not use push turns the module off in app/modules.ini.