Route
in package
Class Route
Represents a route in the HTTP router. Stores the URI pattern and the action (handler) to execute.
Supports parametrized routes using {param}
syntax and extracts parameters from matching URIs.
Table of Contents
Properties
- $action : Closure|array<string|int, mixed>
- The action (handler) to execute when the route matches.
- $middlewares : array<string|int, Middleware>
- HTTP middlewares associated with this route.
- $parameters : array<string|int, string>
- List of parameter names defined in the URI (e.g., ["id"] for /users/{id}).
- $regex : string
- Regular expression derived from the URI pattern.
- $uri : string
- The URI pattern defined for the route (e.g., /users/{id}).
Methods
- __construct() : mixed
- Constructor.
- delete() : self
- Registers a DELETE route with the specified URI and action.
- get() : self
- Registers a GET route with the application's router.
- getAction() : Closure|array<string|int, mixed>
- Get the handler associated with this route.
- getMiddlewares() : array<string|int, Middleware>
- Get HTTP middlewares associated with this route.
- getUri() : string
- Get the URI definition for this route.
- hasMiddlewares() : bool
- Checks if middlewares has been added to the current Route.
- hasParameters() : bool
- Determine whether this route has defined parameters.
- load() : mixed
- matches() : bool
- Check if the given URI matches this route's pattern.
- parseParameters() : array<string, string>
- Parse and extract parameter values from the given URI.
- patch() : self
- Registers a new PATCH route with the application's router.
- post() : self
- Registers a POST route with the specified URI and action.
- put() : self
- Registers a new route that responds to HTTP PUT requests.
- setMiddlewares() : Route
- Set HTTP middlewares for this route.
Properties
$action
The action (handler) to execute when the route matches.
protected
Closure|array<string|int, mixed>
$action
$middlewares
HTTP middlewares associated with this route.
protected
array<string|int, Middleware>
$middlewares
= []
$parameters
List of parameter names defined in the URI (e.g., ["id"] for /users/{id}).
protected
array<string|int, string>
$parameters
$regex
Regular expression derived from the URI pattern.
protected
string
$regex
$uri
The URI pattern defined for the route (e.g., /users/{id}).
protected
string
$uri
Methods
__construct()
Constructor.
public
__construct(string $uri, Closure|array<string|int, mixed> $action) : mixed
Parameters
- $uri : string
-
The route URI definition, possibly with parameters.
- $action : Closure|array<string|int, mixed>
-
The handler to be executed for this route.
delete()
Registers a DELETE route with the specified URI and action.
public
static delete(string $uri, Closure|array<string|int, mixed> $action) : self
Parameters
- $uri : string
-
The URI pattern for the route.
- $action : Closure|array<string|int, mixed>
-
The action to be executed when the route is matched.
Tags
Return values
self —Returns the current instance of the route.
get()
Registers a GET route with the application's router.
public
static get(string $uri, Closure|array<string|int, mixed> $action) : self
Parameters
- $uri : string
-
The URI pattern for the route.
- $action : Closure|array<string|int, mixed>
-
The action to be executed when the route is matched.
Tags
Return values
self —Returns the current instance of the route.
getAction()
Get the handler associated with this route.
public
getAction() : Closure|array<string|int, mixed>
Return values
Closure|array<string|int, mixed>getMiddlewares()
Get HTTP middlewares associated with this route.
public
getMiddlewares() : array<string|int, Middleware>
Return values
array<string|int, Middleware>getUri()
Get the URI definition for this route.
public
getUri() : string
Return values
stringhasMiddlewares()
Checks if middlewares has been added to the current Route.
public
hasMiddlewares() : bool
Return values
bool —Returns true if there are middlewares associated with the route, false otherwise.
hasParameters()
Determine whether this route has defined parameters.
public
hasParameters() : bool
Return values
bool —True if parameters exist; false otherwise.
load()
public
static load(string $routesDirectory) : mixed
Parameters
- $routesDirectory : string
matches()
Check if the given URI matches this route's pattern.
public
matches(string $uri) : bool
Parameters
- $uri : string
-
The request URI.
Return values
bool —True if it matches; false otherwise.
parseParameters()
Parse and extract parameter values from the given URI.
public
parseParameters(string $uri) : array<string, string>
Parameters
- $uri : string
-
The URI to extract values from.
Return values
array<string, string> —Associative array of parameter names and their values.
patch()
Registers a new PATCH route with the application's router.
public
static patch(string $uri, Closure|array<string|int, mixed> $action) : self
Parameters
- $uri : string
-
The URI pattern for the route.
- $action : Closure|array<string|int, mixed>
-
The action to be executed when the route is matched.
Tags
Return values
self —Returns the current instance for method chaining.
post()
Registers a POST route with the specified URI and action.
public
static post(string $uri, Closure|array<string|int, mixed> $action) : self
Parameters
- $uri : string
-
The URI pattern for the route.
- $action : Closure|array<string|int, mixed>
-
The action to be executed when the route is matched.
Tags
Return values
self —Returns the current instance of the route for method chaining.
put()
Registers a new route that responds to HTTP PUT requests.
public
static put(string $uri, Closure|array<string|int, mixed> $action) : self
Parameters
- $uri : string
-
The URI pattern for the route.
- $action : Closure|array<string|int, mixed>
-
The action to be executed when the route is matched.
Tags
Return values
self —Returns the current instance of the route for method chaining.
setMiddlewares()
Set HTTP middlewares for this route.
public
setMiddlewares(array<string|int, string> $middlewares) : Route
Parameters
- $middlewares : array<string|int, string>
-
Array of middleware class names.