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 |
|---|---|---|
| core | Index, Auth, User, Admin, Push, Entity | on |
| site | every module not named here | on |
| example | Module, HelloWorld, Test | off |
- No module is always on. Any of the nine can be turned off,
IndexandAdminincluded. - The tier goes by name (
Modules::tier()), so a site module that overrides a module of core keeps thecoretier. AccessRequestis a submodule of Auth, so it is on when Auth is on.#[RequireLogin]redirects to/user/loginand#[RequirePermission]to/auth/access-request/permissionwhatever is switched on. On a site that turned User or Auth off, those URLs answer 404.
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
[enabled]takes a boolean per module name. Names are compared ignoring case and hyphens.[order]sets the position of a module's card on the admin dashboard: lower first, no entry last.- An entry for a name that matches no module is ignored.
- A missing file means all defaults. A file that does not parse is read as all defaults and logs a warning naming it.
- Core reads the file once per request, so a change takes effect on the next one.
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.
- Only symlinks are removed: those under
{WEB_ROOT}/assets/, and one level below each real directory there, whose real path lies inside a disabled module's folder. That covers a link of any spelling and a link into a submodule. - A real directory, a link into a module that is on, and a link pointing outside every module stay.
- The marker is written only when every removal succeeded. When
storage/cache/cannot be written, the cleanup runs on every request.
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/:
- The site's class answers the URL. It is in the site's namespace,
Zero\Module\Auth;Modules::controllerClass()gives the class for a name. - The site's folder is the one
Modules::path()names, so its views, assets, strings, config andAdmin/are the ones used. Nothing in core's folder is read on its behalf. - Core's classes stay loadable under their own names, so the site's class may extend core's or stand alone.
Modules::overridden()says a name is held by both roots;Modules::corePath()andModules::sitePath()name each folder;Modules::isCore()says whether the module that answers is core's own.
<?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()):
- The request fails with a bare
500and the textInternal Server Error. Console::error()logsModule name clash: ... and ... are the same module name, naming both paths. The message is never shown to a visitor.
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:
- writes only departures from the defaults, and keeps names in the file that no module has;
- refuses an unknown name, an order outside 0 to 9999, and a module named like an INI keyword (
On,Off,Yes,No,True,False,None,Null); - rejects an existing file that holds anything but the two sections with booleans and whole numbers, naming the file and the key;
- saves under a lock on
app/modules.ini.lock, writing a temp file in the same folder and renaming it over the original, so a reader sees the old file or the new one; - refuses the save when the file changed since it was read. The screen's form carries the file's hash, and a stale post is answered with 409.
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/subscribe | required | Saves the browser's subscription |
POST /push/unsubscribe | required | Removes it |
GET /push/status | required | subscribed and count for the signed-in user |
POST /push/click | required | Records a click |
GET /push/vapid-key | public | The 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.