phpscript

manual

ClassGo

HTTP\Mux

Description

HTTP\Mux routes requests to the PHP functions that answer them. The mux is net/http's ServeMux and $mux->handle("GET /users/{id}", $fn) takes the patterns Go takes, and it calls a handler with the response writer and the request themselves.

HTTP\Mux is a class phpscript declares.

$object = new HTTP\Mux();

Methods

public function group(mixed $fn): void

group calls $fn with a mux that shares this one's routes and starts with its middleware. What $fn passes to Use stays inside the group, which is how a module isolates its routes from the rest of the router.

The go method Group.

public function handle(string $pattern, mixed $handler): void

handle registers $handler for $pattern, which is ServeMux.HandleFunc's pattern, with {name} segments the handler reads through $r->path_value($name).

$handler is a callable: a closure, a method read off its receiver as $this->fnName, or the name of a declared function. The mux calls it with the response writer and the request, what it echoes reaches the response, and a throw becomes a 500. docs/use-cases/http-server.md is the contract.

The go method Handle.

public function serve_http(object $w, object $r): void

serve_http answers one request, the script listening on nothing.

The go method ServeHTTP.

public function use(mixed $middleware): void

use wraps the handlers registered after it with $middleware, chi's shape.

The go method Use.

public function use_pool_for_test(object $pool): void

use_pool_for_test is usePool under a name a test can reach.

The go method UsePoolForTest.

Binding

See also