Router
in package
Class Router
A simple HTTP router that maps URIs and HTTP methods to handlers. Supports typical RESTful HTTP methods and route resolution.
Table of Contents
Properties
- $routes : array<string, array<string|int, Route>>
- The registered HTTP routes, organized by HTTP method.
Methods
- __construct() : mixed
- Router constructor.
- delete() : Route
- Registers a DELETE route.
- get() : Route
- Registers a GET route.
- patch() : Route
- Registers a PATCH route.
- post() : Route
- Registers a POST route.
- put() : Route
- Registers a PUT route.
- resolve() : Response
- Resolves the given request to a corresponding route and executes the route's action.
- resolveRoute() : Route
- Resolves a request to a matching route.
- execMiddlewares() : Response
- Executes all middlewares before launch the handler
- registerRoute() : Route
- Registers a new route for a specific HTTP method.
Properties
$routes
The registered HTTP routes, organized by HTTP method.
protected
array<string, array<string|int, Route>>
$routes
= []
An associative array where each key is an HTTP method name (e.g. "GET") and the value is an array of Route objects for that method.
Methods
__construct()
Router constructor.
public
__construct() : mixed
Initializes the routes array for each available HTTP method.
delete()
Registers a DELETE route.
public
delete(string $uri, Closure|array<string|int, mixed> $action) : Route
Parameters
- $uri : string
- $action : Closure|array<string|int, mixed>
Return values
Routeget()
Registers a GET route.
public
get(string $uri, Closure|array<string|int, mixed> $action) : Route
Parameters
- $uri : string
- $action : Closure|array<string|int, mixed>
Return values
Routepatch()
Registers a PATCH route.
public
patch(string $uri, Closure|array<string|int, mixed> $action) : Route
Parameters
- $uri : string
- $action : Closure|array<string|int, mixed>
Return values
Routepost()
Registers a POST route.
public
post(string $uri, Closure|array<string|int, mixed> $action) : Route
Parameters
- $uri : string
- $action : Closure|array<string|int, mixed>
Return values
Routeput()
Registers a PUT route.
public
put(string $uri, Closure|array<string|int, mixed> $action) : Route
Parameters
- $uri : string
- $action : Closure|array<string|int, mixed>
Return values
Routeresolve()
Resolves the given request to a corresponding route and executes the route's action.
public
resolve(Request $request) : Response
Parameters
- $request : Request
-
The incoming HTTP request to be resolved.
Tags
Return values
Response —The HTTP response generated by the resolved route's action.
resolveRoute()
Resolves a request to a matching route.
public
resolveRoute(Request $request) : Route
Parameters
- $request : Request
-
The HTTP request instance.
Tags
Return values
Route —The matched route instance.
execMiddlewares()
Executes all middlewares before launch the handler
protected
execMiddlewares(Request $request, array<string|int, Middleware> $middlewares, Closure|array<string|int, mixed> $target) : Response
Parameters
- $request : Request
- $middlewares : array<string|int, Middleware>
- $target : Closure|array<string|int, mixed>
Return values
ResponseregisterRoute()
Registers a new route for a specific HTTP method.
protected
registerRoute(HttpMethod $method, string $uri, Closure $action) : Route
Parameters
- $method : HttpMethod
-
The HTTP method (GET, POST, etc.)
- $uri : string
-
The route URI (e.g. "/users")
- $action : Closure
-
The handler to be executed for this route