Lapis Docs

Data and code

Middleware

Middleware runs before a page and may answer in its place: it asks for a password, sets headers, limits requests or changes the cache rules. In Lapis, you attach middleware to folders. A _middleware.php in any folder of pages/ applies to every URL below it, and the folders inherit it from the root down.

<?php // pages/clients/_middleware.php
use Lapis\Middleware\BasicAuth;
use Lapis\Middleware\NoIndex;
use Tempest\Http\Request;

return [
    new BasicAuth('client', '$2y$12$…'),      // plain password or password_hash()
    NoIndex::class,
    fn (Request $request, Closure $next) => $next($request)->addHeader('X-Area', 'clients'),
];

An item of the list may be a middleware object, a class name or a closure. They run in the order of the folders, from the root down, and within a folder in the order of the list. ./lapis routes shows which middleware a route gets.

For one path

A key in the list names a path relative to the folder, and its middleware only applies there. In a key, * matches anything, / too:

<?php // pages/_middleware.php
use Lapis\Middleware\{Cache, NoIndex, Skip};

return [
    new NoIndex(),
    'prices' => [new Cache(ttl: 300)],
    'archive/*' => new Skip(NoIndex::class),   // drop an inherited middleware
];

Built-in middleware

All of these are in Lapis\Middleware, and each also works in a page function's #[Middleware(…)] attribute:

Middleware What it does
RequireEditor Lets signed-in users in only.
RequireRole(Role::Admin) Lets signed-in users with this role in only. Admins always pass.
BasicAuth(user, password, realm:, allowEditors:) Asks for a password, given plain or as a password_hash(). Editors pass by default (allowEditors: false asks them too).
Headers([...], replace:) Adds response headers, for example the site's own Content-Security-Policy. Replaces existing headers by default (replace: false keeps the page's own).
NoIndex Hides the page from search engines and from the sitemap.
Skip(Class::class, …) Drops one or more middleware classes inherited from an outer folder (closures cannot be skipped).
Cache(enabled:, ttl:, bypassCookies:) Sets the page cache rules for guests.
RateLimit(max:, seconds:, methods:, key:, name:) Allows at most max requests per window, per IP or per your own key, counted in the database. Above it, the answer is a 429 with Retry-After. name: separates multiple limiters on the same path.
Cors(origins:, methods:, …) Lets other origins call the pages with fetch(), see below.

CORS

Cors(origins:, methods:, headers:, expose:, credentials:, maxAge:) answers the browser's preflight request (OPTIONS) itself, and adds the Access-Control-* headers to the answers:

<?php // pages/api/_middleware.php
use Lapis\Middleware\Cors;

return [
    new Cors(origins: ['https://app.example.com', 'https://*.example.com'], methods: ['GET', 'POST'], credentials: true),
    'feed' => new Cors(origins: ['*']),   // public, read-only
];

A listed origin also passes the cross-site check of page functions for the given methods. Its fetch() POST reaches a #[Post] function without #[CrossSite].

Redirects and maintenance

Redirects need no middleware. Lapis applies them to every page by itself, both for moved pages and for the list in the SEO panel.

Maintenance mode is a console command. ./lapis down --message="…" --retry=600 turns it on (with a Retry-After in seconds), and ./lapis up turns it off again. Meanwhile visitors get a 503, from your own pages/_maintenance.php if there is one, and editors keep working.

Your own middleware

Your own middleware may be a closure (fn (Request $request, Closure $next) => …), an HttpMiddleware instance or a class name.