Router
in package
URL router with regex pattern matching.
Maps HTTP method + URI patterns to controller actions with optional middleware.
Tags
Table of Contents
Properties
- $routes : array<string|int, mixed>
Methods
- delete() : self
- Register a DELETE route.
- dispatch() : void
- Dispatch an incoming request to the matched route.
- get() : self
- Register a GET route.
- getRoutes() : array<string|int, mixed>
- Get all registered routes (useful for debugging).
- patch() : self
- Register a PATCH route.
- post() : self
- Register a POST route.
- put() : self
- Register a PUT route.
- addRoute() : self
- Add a route to the routing table.
- sendError() : void
- Send an HTTP error response.
Properties
$routes
private
array<string|int, mixed>
$routes
= []
Registered routes grouped by HTTP method
Methods
delete()
Register a DELETE route.
public
delete(string $path, string $controller, string $action[, array<string|int, mixed> $middleware = [] ]) : self
Parameters
- $path : string
-
URI pattern
- $controller : string
-
Controller class name
- $action : string
-
Method name
- $middleware : array<string|int, mixed> = []
-
Middleware class names
Return values
selfdispatch()
Dispatch an incoming request to the matched route.
public
dispatch(string $method, string $uri) : void
Parameters
- $method : string
-
HTTP method (GET, POST, etc.)
- $uri : string
-
Request URI
get()
Register a GET route.
public
get(string $path, string $controller, string $action[, array<string|int, mixed> $middleware = [] ]) : self
Parameters
- $path : string
-
URI pattern (e.g., "admin/businesses/{id}/mode")
- $controller : string
-
Fully qualified controller class name
- $action : string
-
Method name on the controller
- $middleware : array<string|int, mixed> = []
-
Array of middleware class names
Return values
selfgetRoutes()
Get all registered routes (useful for debugging).
public
getRoutes() : array<string|int, mixed>
Return values
array<string|int, mixed>patch()
Register a PATCH route.
public
patch(string $path, string $controller, string $action[, array<string|int, mixed> $middleware = [] ]) : self
Parameters
- $path : string
-
URI pattern
- $controller : string
-
Controller class name
- $action : string
-
Method name
- $middleware : array<string|int, mixed> = []
-
Middleware class names
Return values
selfpost()
Register a POST route.
public
post(string $path, string $controller, string $action[, array<string|int, mixed> $middleware = [] ]) : self
Parameters
- $path : string
-
URI pattern
- $controller : string
-
Controller class name
- $action : string
-
Method name
- $middleware : array<string|int, mixed> = []
-
Middleware class names
Return values
selfput()
Register a PUT route.
public
put(string $path, string $controller, string $action[, array<string|int, mixed> $middleware = [] ]) : self
Parameters
- $path : string
-
URI pattern
- $controller : string
-
Controller class name
- $action : string
-
Method name
- $middleware : array<string|int, mixed> = []
-
Middleware class names
Return values
selfaddRoute()
Add a route to the routing table.
private
addRoute(string $method, string $path, string $controller, string $action, array<string|int, mixed> $middleware) : self
Parameters
- $method : string
-
HTTP method
- $path : string
-
URI pattern
- $controller : string
-
Controller class name
- $action : string
-
Method name
- $middleware : array<string|int, mixed>
-
Middleware class names
Return values
selfsendError()
Send an HTTP error response.
private
sendError(int $code, string $message) : void
Parameters
- $code : int
-
HTTP status code
- $message : string
-
Error message