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.
- The module and endpoint segments are lowercased, so
/Hello-World/Say-Helloand/hello-world/say-helloare the same route. Arguments are passed as written. - A segment with hyphens becomes camelCase:
say-hellois the methodsayHello(), andhello-worldis the moduleHelloWorld. - A path of one segment gets the endpoint
index. - An empty path is
index/index: theIndexmodule'sindex().
| 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:
What happens, step by step (core/Application.php, core/Module.php, core/Response.php):
new Application()starts output buffering and the PHP session, and stamps a new session with the time it was created.registerAutoloaders()tellsModuleswhere the module folders are, registers class loading and loads the plugins.parseRequest()buildsRequestfrom the URL, as described above.defineConstants()reads the strings and config files, including the requested module's own (Configuration). It ends by giving a session older thanSESSION_REGENERATE_INTERVALseconds a new session id (Security).run()begins withbanNefariousRequest()(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()).run()loads the module's controller when the module exists and is on. With no such module the request is served by a plainResponse, andIndexis loaded for step 10.- The module is instantiated and
Application::checkAttributes()runs the attributes on the class and the method. - If the method exists and is public:
$module->{$endpoint}(...$args). - If it does not,
Module::__call()looks for a submodule at{Module}/submodule/{Endpoint}/{Endpoint}.php. - With no submodule,
Response::__call()looks for a view or a standalone page, and throwsHTTPError(404)when there is neither. See When no method matches. Responsewraps 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.
- Declare a method
publiconly when a URL may call it. Every helper isprotectedorprivate, static helpers included. - The helpers of
ResponseandModuleare protected for this reason:respond(),build(),export(),success(),error(),getStylesheets(),getScripts(),getComponents(),getAuxiliary(),framePiecePath(),framePieceFile(),findModuleView(),adoptFrameFrom()andlinkAssets(). A frame or a view still calls them as$this->...: it is included from inside the object, so a protected method is in reach. renderError()is public, becauseApplication::run()calls it.- An admin screen has a stricter rule of its own,
AdminResponse::isRoutableMethod().
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:
- 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); - a public method of the
Indexmodule named after the first URL segment; - a view file
Index/view/{segment}.phpnamed after that same segment; 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');
}
}
- A method beats a view. When
Indexhas both anabout()method andview/about.php, the method is the page and the file is its template. A page that needs no data needs no method:view/about.phpalone answers/about. - A module's own view beats both, so a real module's view is never replaced by a same-named page in
Index. - Only public, non-static methods
Indexitself declares are routable this way. A method it inherits fromModuleorResponseis not, and neither is a name that starts with__. - The method goes through
Application::checkAttributes()like any other endpoint, beforeIndexis constructed, and is called with no arguments. - A standalone page's assets are
Index's:/aboutloadsabout.cssfrom theIndexmodule. See Assets.
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::$acceptsJSON | false | True when an Accept entry is a JSON type: application/json, with or without a ;q= value, or a +json type |
Request::$madeWithAJAX | false | True 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. |