HttpRouter
in package
implements
IRouter
uses
RouterTrait
Table of Contents
Interfaces
Methods
- __construct() : mixed
- add() : void
- Registers a route handler for a given method and path.
- delete() : void
- dispatch() : void
- Dispatches a route based on a given HTTP request message with additional prefix arguments and output to stdout.
- get() : void
- getCharSet() : string
- Retrieves the normalised name of the preferred character set.
- getErrorHandler() : IErrorHandler
- Retrieves the error handler instance.
- options() : void
- output() : void
- Outputs a HTTP response message to stdout.
- patch() : void
- post() : void
- put() : void
- register() : void
- registerContentHandler() : void
- Register a message body content handler.
- registerDefaultContentHandlers() : void
- Register the default content handlers.
- resolve() : ResolvedRouteInfo
- Resolves middlewares and a route handler for a given method and path.
- scopeTo() : IRouter
- Retrieve a scoped router to a given path prefix.
- setErrorHandler() : void
- Changes the active error handler.
- setHTMLErrorHandler() : void
- Set the error handler to the basic HTML one.
- setPlainErrorHandler() : void
- Set the error handler to the plain text one.
- use() : void
- Registers a middleware handler.
- writeErrorPage() : void
- Writes an error page to a given HTTP response builder.
Methods
__construct()
public
__construct([string $charSet = '' ][, IErrorHandler|string $errorHandler = 'html' ][, bool $registerDefaultContentHandlers = true ]) : mixed
Parameters
- $charSet : string = ''
-
Default character set to specify when none is present.
- $errorHandler : IErrorHandler|string = 'html'
-
Error handling to use for error responses with an empty body. 'html' for the default HTML implementation, 'plain' for the plaintext implementation.
- $registerDefaultContentHandlers : bool = true
-
true to register default content handlers for JSON, Bencode, etc.
add()
Registers a route handler for a given method and path.
public
add(string $method, string $path, callable $handler) : void
Parameters
- $method : string
-
Method to use this handler for.
- $path : string
-
Path or regex to use this handler with.
- $handler : callable
-
Handler to use for this method/path combination.
Tags
delete()
public
delete(string $path, callable $handler) : void
Parameters
- $path : string
- $handler : callable
dispatch()
Dispatches a route based on a given HTTP request message with additional prefix arguments and output to stdout.
public
dispatch([HttpRequest|null $request = null ][, array<string|int, mixed> $args = [] ]) : void
Parameters
- $request : HttpRequest|null = null
-
HTTP request message to handle, null to use the current request.
- $args : array<string|int, mixed> = []
-
Additional arguments to prepend to the argument list sent to the middleware and route handlers.
get()
public
get(string $path, callable $handler) : void
Parameters
- $path : string
- $handler : callable
getCharSet()
Retrieves the normalised name of the preferred character set.
public
getCharSet() : string
Return values
string —Normalised character set name.
getErrorHandler()
Retrieves the error handler instance.
public
getErrorHandler() : IErrorHandler
Return values
IErrorHandler —The error handler.
options()
public
options(string $path, callable $handler) : void
Parameters
- $path : string
- $handler : callable
output()
Outputs a HTTP response message to stdout.
public
static output(HttpResponse $response, bool $includeBody) : void
Parameters
- $response : HttpResponse
-
HTTP response message to output.
- $includeBody : bool
-
true to include the response message body, false to omit it for HEAD requests.
patch()
public
patch(string $path, callable $handler) : void
Parameters
- $path : string
- $handler : callable
post()
public
post(string $path, callable $handler) : void
Parameters
- $path : string
- $handler : callable
put()
public
put(string $path, callable $handler) : void
Parameters
- $path : string
- $handler : callable
register()
public
register(IRouteHandler $handler) : void
Parameters
- $handler : IRouteHandler
registerContentHandler()
Register a message body content handler.
public
registerContentHandler(IContentHandler $contentHandler) : void
Parameters
- $contentHandler : IContentHandler
-
Content handler to register.
registerDefaultContentHandlers()
Register the default content handlers.
public
registerDefaultContentHandlers() : void
resolve()
Resolves middlewares and a route handler for a given method and path.
public
resolve(string $method, string $path) : ResolvedRouteInfo
Parameters
- $method : string
-
Method to resolve for.
- $path : string
-
Path to resolve for.
Return values
ResolvedRouteInfo —Resolved route information.
scopeTo()
Retrieve a scoped router to a given path prefix.
public
scopeTo(string $prefix) : IRouter
Parameters
- $prefix : string
-
Prefix to apply to paths within the returned router.
Return values
IRouter —Scopes router proxy.
setErrorHandler()
Changes the active error handler.
public
setErrorHandler(IErrorHandler|string $handler) : void
Parameters
- $handler : IErrorHandler|string
-
Error handling to use for error responses with an empty body. 'html' for the default HTML implementation, 'plain' for the plaintext implementation.
setHTMLErrorHandler()
Set the error handler to the basic HTML one.
public
setHTMLErrorHandler() : void
setPlainErrorHandler()
Set the error handler to the plain text one.
public
setPlainErrorHandler() : void
use()
Registers a middleware handler.
public
use(string $path, callable $handler) : void
Parameters
- $path : string
-
Path prefix or regex to apply this middleware on.
- $handler : callable
-
Middleware handler.
writeErrorPage()
Writes an error page to a given HTTP response builder.
public
writeErrorPage(HttpResponseBuilder $response, HttpRequest $request, int $statusCode) : void
Parameters
- $response : HttpResponseBuilder
-
HTTP response builder to apply the error page to.
- $request : HttpRequest
-
HTTP request that triggered this error.
- $statusCode : int
-
HTTP status code for this error page.