Paths
in package
Class for managing routes using handlers.
HTTP router class for defining and handling application routes. Supports RESTful routing with dynamic parameters, route groups, resource routes, and automatic parameter extraction. Handles GET, POST, PUT, and DELETE methods.
Table of Contents
Properties
- $prefixDiscriminator : string
- Regular expression pattern to identify and remove API prefixes from paths
- $routes : array<string|int, mixed>
- Collection of registered routes organized by HTTP method
Methods
- __construct() : mixed
- Constructor
- delete() : void
- Registers a DELETE route
- get() : void
- Registers a GET route
- group() : void
- Creates a route group with a common prefix
- post() : void
- Registers a POST route
- put() : void
- Registers a PUT route
- resource() : void
- Registers a RESTful resource with standard CRUD routes
- run() : void
- Dispatches the current request to the matching route
- url() : string
- Generates a URL by replacing route parameters with actual values
- addRoute() : void
- Adds a route to the routes collection
- executeRoute() : void
- Executes a route handler with extracted parameters
- getPath() : string
- Extracts and normalizes the request path
Properties
$prefixDiscriminator
Regular expression pattern to identify and remove API prefixes from paths
private
string
$prefixDiscriminator
= '/^\/api\//'
$routes
Collection of registered routes organized by HTTP method
private
array<string|int, mixed>
$routes
= []
Methods
__construct()
Constructor
public
__construct(string $prefixDiscriminator) : mixed
Initializes the router with a custom prefix discriminator pattern.
Parameters
- $prefixDiscriminator : string
-
Regular expression to match and remove path prefixes
delete()
Registers a DELETE route
public
delete(string $path, callable|array<string|int, mixed> $handler) : void
Parameters
- $path : string
-
Route path with optional dynamic parameters in {param} format
- $handler : callable|array<string|int, mixed>
-
Callback function or array [controller, method] to handle the route
get()
Registers a GET route
public
get(string $path, callable|array<string|int, mixed> $handler) : void
Parameters
- $path : string
-
Route path with optional dynamic parameters in {param} format
- $handler : callable|array<string|int, mixed>
-
Callback function or array [controller, method] to handle the route
group()
Creates a route group with a common prefix
public
group(string $prefix, callable $callback) : void
Allows organizing related routes under a shared path prefix. Routes defined in the callback are automatically prefixed.
Parameters
- $prefix : string
-
Common path prefix for all routes in the group
- $callback : callable
-
Function that defines routes within the group
post()
Registers a POST route
public
post(string $path, callable|array<string|int, mixed> $handler) : void
Parameters
- $path : string
-
Route path with optional dynamic parameters in {param} format
- $handler : callable|array<string|int, mixed>
-
Callback function or array [controller, method] to handle the route
put()
Registers a PUT route
public
put(string $path, callable|array<string|int, mixed> $handler) : void
Parameters
- $path : string
-
Route path with optional dynamic parameters in {param} format
- $handler : callable|array<string|int, mixed>
-
Callback function or array [controller, method] to handle the route
resource()
Registers a RESTful resource with standard CRUD routes
public
resource(string $resource, object $controller) : void
Automatically creates five standard routes:
- GET /resource -> index
- GET /resource/{id} -> show
- POST /resource -> store
- PUT /resource/{id} -> update
- DELETE /resource/{id} -> destroy
Parameters
- $resource : string
-
Resource name/path
- $controller : object
-
Controller instance that handles the resource
run()
Dispatches the current request to the matching route
public
run() : void
Matches the current HTTP method and URI against registered routes, extracts dynamic parameters, and executes the corresponding handler. Returns a 404 response if no matching route is found.
Return values
void —Executes route handler or sends error response
url()
Generates a URL by replacing route parameters with actual values
public
url(string $path[, array<string|int, mixed> $params = [] ]) : string
Useful for creating URLs dynamically based on route definitions.
Parameters
- $path : string
-
Route path template with {param} placeholders
- $params : array<string|int, mixed> = []
-
Associative array of parameter names and values
Return values
string —Generated URL with parameters replaced
addRoute()
Adds a route to the routes collection
private
addRoute(string $method, string $path, callable|array<string|int, mixed> $handler) : void
Converts route path with {param} syntax to a regular expression pattern for matching and parameter extraction.
Parameters
- $method : string
-
HTTP method (GET, POST, PUT, DELETE)
- $path : string
-
Route path with optional dynamic parameters
- $handler : callable|array<string|int, mixed>
-
Route handler
executeRoute()
Executes a route handler with extracted parameters
private
executeRoute(callable|array<string|int, mixed> $handler[, array<string|int, mixed> $params = [] ]) : void
Supports both controller method arrays and anonymous functions as handlers. Parameters are passed to the handler as an associative array.
Parameters
- $handler : callable|array<string|int, mixed>
-
Route handler (callback or [controller, method])
- $params : array<string|int, mixed> = []
-
Extracted route parameters
Return values
void —Executes handler or sends error response
getPath()
Extracts and normalizes the request path
private
getPath() : string
Removes query strings and API prefixes from the request URI.
Return values
string —Normalized request path