ClassGo
HTTP\Mux
class HTTP\Mux {
- public function __construct();
- public function group(mixed $fn): void;
- public function handle(string $pattern, mixed $handler): void;
- public function serve_http(object $w, object $r): void;
- public function use(mixed $middleware): void;
- public function use_pool_for_test(object $pool): void;
}
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
- The go type
github.com/titpetric/phpscript/stdlib/http.Mux. - Registered by the package
github.com/titpetric/phpscript/stdlib/http. - Built on
net/http.ServeMux. - Satisfies
net/http.Handler.