stave.dev

Routing

A URL names a class and a method. There is no route file: the folder a module sits in and the public methods it declares are the routing table.

URL-to-Class Mapping

core/Request.php splits the path of every URL into three parts: module, endpoint and arguments. The query string is not part of the route.

/module
/endpoint
/arg1/arg2/...argN
?query=string
URL Class Method Call
/ Index Index::index()
/hello-world HelloWorld HelloWorld::index()
/hello-world/say-hello HelloWorld HelloWorld::sayHello()
/user-profile/edit/42 UserProfile UserProfile::edit('42')
/auth/login Auth Auth::login()
/api/users/5/posts Api Api::users('5', 'posts')

Which classes a URL can load

A URL loads a class only if it is a module: a folder in the site's modules/ or in core/modules/ that is switched on, holding a controller file of the same name (Application::controllerFile()). A site module with the same name as one of core's answers in its place. A module that is off is answered exactly as one that does not exist. Bundled Modules covers how a module is found, the tiers and app/modules.ini.

Request Flow

The web root's index.php defines four constants (ZERO_ROOT, ROOT_PATH, VIEW_PATH, MODULE_PATH), creates the Application and calls four methods on it in order:

registerAutoloaders()
→
parseRequest()
→
defineConstants()
→
run()

What happens, step by step (core/Application.php, core/Module.php, core/Response.php):

  1. new Application() starts output buffering and the PHP session, and stamps a new session with the time it was created.
  2. registerAutoloaders() tells Modules where the module folders are, registers class loading and loads the plugins.
  3. parseRequest() builds Request from the URL, as described above.
  4. defineConstants() reads the strings and config files, including the requested module's own (Configuration). It ends by giving a session older than SESSION_REGENERATE_INTERVAL seconds a new session id (Security).
  5. run() begins with banNefariousRequest() (see Security) and the check on the module folders: two spellings of one module name, or one name in two folders of the site, is a configuration error, and the request ends with a bare 500. A disabled account is logged out here (Session::endIfDisabled()).
  6. run() loads the module's controller when the module exists and is on. With no such module the request is served by a plain Response, and Index is loaded for step 10.
  7. The module is instantiated and Application::checkAttributes() runs the attributes on the class and the method.
  8. If the method exists and is public: $module->{$endpoint}(...$args).
  9. If it does not, Module::__call() looks for a submodule at {Module}/submodule/{Endpoint}/{Endpoint}.php.
  10. With no submodule, Response::__call() looks for a view or a standalone page, and throws HTTPError(404) when there is neither. See When no method matches.
  11. Response wraps the view in the frame: head, header, sideNav, footer. A JSON client gets no frame and no view; an AJAX request gets the view without the frame. See Views & Responses.

An HTTPError thrown anywhere in steps 7 to 10 is caught by run() and rendered by the module that was instantiated. See Error Handling.

Argument Handling

The URL segments after the endpoint are passed as separate arguments, so an endpoint declares named parameters, not a single $args array:

// URL: /hello-world/say-hello/alice/bob
// Framework calls: $module->sayHello(...['alice', 'bob'])
// Which is equivalent to: $module->sayHello('alice', 'bob')

public function sayHello($name1, $name2 = null) {
    // $name1 = 'alice'
    // $name2 = 'bob'
}
The URL has Result
More segments than the method has parameters The extra segments are ignored
No segment for an optional parameter ($param = null) The parameter takes its default
No segment for a required parameter PHP raises ArgumentCountError, which Application::run() answers with HTTP 400

Recommended Pattern

// Required argument: /module/show/42
public function show($id) {
    $item = $this->model->find($id);
    if (!$item) {
        throw new HTTPError(404, 'Item not found');
    }
}

// Optional argument: /module/list or /module/list/active
public function list($filter = null) {
    // $filter is 'active' or null
}

// Multiple arguments: /module/edit/5/settings
public function edit($id, $tab = null) {
    // $id = '5', $tab = 'settings' or null
}

A Public Method Is an Endpoint

Step 8 calls the method from outside the module object, so PHP runs any public method the URL names: one the module declares, and one it inherits. A protected or private method is not reached that way. PHP hands its name to __call() instead, which finds a submodule, a view or a 404.

class Report extends \Zero\Core\Module {

    // /report/monthly/2026-11 runs this
    public function monthly($month) {
        $this->data = ['rows' => $this->rowsFor($month)];
        $this->respond($this->viewPath . 'monthly.php');
    }

    // /report/rows-for is a 404: the method is not public
    private function rowsFor($month) {
        // ...
    }
}

When No Method Matches

When neither a public method nor a submodule answers the URL, Response::__call() takes the first of:

  1. a view file in the requested module's own folder, {Module}/view/{endpoint}.php, named as the method would be (sayHello.php) or as the URL is (say-hello.php);
  2. a public method of the Index module named after the first URL segment;
  3. a view file Index/view/{segment}.php named after that same segment;
  4. HTTPError(404).

The first is the view-only endpoint: /docs/faq renders modules/Docs/view/faq.php with no faq() method. It reads the first two URL segments, so it serves a top-level module only; an endpoint of a submodule needs a method.

Standalone pages

The second and third are the standalone page: a URL such as /about with no About module behind it. It belongs to Index.

<?php
namespace Zero\Module;

class Index extends \Zero\Core\Module {

    public function index() {
        $this->respond($this->viewPath . 'index.php');
    }

    // Answers /about
    public function about() {
        $this->data = ['team' => ['Ada', 'Grace']];
        $this->respond($this->viewPath . 'about.php');
    }
}

The Request Object

After parsing, the request is available as static properties of \Zero\Core\Request. The example values are for https://stave.dev/hello-world/say-hello/alice/bob:

Property Example Value Description
Request::$module'helloWorld'First segment, lowercased, hyphens to camelCase
Request::$Module'HelloWorld'The same with a leading capital
Request::$moduleOrig'hello-world'First segment, lowercased, hyphens kept
Request::$endpoint'sayHello'Second segment, lowercased, hyphens to camelCase; 'index' when the URL has none
Request::$endpointOrig'say-hello'Second segment, lowercased, hyphens kept
Request::$args['alice', 'bob']The remaining segments, as written
Request::$uri'/hello-world/say-hello/alice/bob'The path without the query string; /index is appended to a one-segment path
Request::$uriArray['hello-world', 'say-hello', 'alice', 'bob']The path split on /
Request::$method'GET'HTTP method
Request::$protocol'https''https' or 'http'
Request::$domain'stave.dev'The Host header as sent
Request::$sub, Request::$subdomain''The host labels before the last two ('www' for www.stave.dev)
Request::$tld'dev'The last host label
Request::$accepts'text/html,...'The Accept header, or null
Request::$acceptsJSONfalseTrue when an Accept entry is a JSON type: application/json, with or without a ;q= value, or a +json type
Request::$madeWithAJAXfalseTrue when X-Requested-With is XMLHttpRequest

A request body sent with Content-Type: application/json is decoded into $_POST. A body that is not valid JSON ends the request with a 400.

Request also has four static methods:

Method What it does
Request::clientIp(): string The client's address. CF-Connecting-IP and X-Forwarded-For are believed only when the peer is a Cloudflare edge; otherwise it is REMOTE_ADDR.
Request::clientIpIsVerified(): bool Whether that address is safe to base an access decision on. False when it came from X-Forwarded-For alone.
Request::redirect($url) Sends a Location header, discards the buffered output and exits. It does not check where $url points.
Request::isSiteRelative(?string $url): bool True for a path on this site (/notes/list), false for anything a browser could read a host out of (//evil.example/x, https://...). Check a URL that came from the request with it before redirecting.