Neuron-PHP

RouteMap
in package

Route mapping and parameter extraction for HTTP requests.

This class represents a single route definition within the routing system, handling URL pattern matching, dynamic parameter extraction, and route execution with integrated filter support.

Key responsibilities:

  • Parse route templates with dynamic parameters (e.g., /users/:id)
  • Extract parameters from incoming URIs that match the route pattern
  • Execute the associated callable with extracted parameters
  • Integrate with the filter system for pre/post processing
  • Handle route-specific parameter validation and type conversion

Route patterns support:

  • Static segments: /users/profile
  • Dynamic parameters: /users/:id, /posts/:slug
  • Mixed patterns: /users/:id/posts/:post_id
Tags
example
// Create a route for user profile
$route = new RouteMap('/users/:id', function($id) {
    return UserController::show($id);
}, 'auth');

// Execute with URI /users/123
$result = $route->execute('/users/123');

Table of Contents

Properties

$Filters  : array<string|int, mixed>
$Function  : mixed
$Name  : string
$Parameters  : array<string|int, mixed>
$Path  : string
$Payload  : array<string|int, mixed>
$_method  : string|null
$_router  : Router|null

Methods

__construct()  : mixed
RouteMap constructor.
execute()  : mixed
getFilter()  : string|null
Legacy method for backward compatibility
getFilters()  : array<string|int, mixed>
Get filters for this route
getFunction()  : callable
getName()  : mixed
getParameters()  : null
getPath()  : string
parseParams()  : array<string|int, mixed>
Extracts the template array from the route definition.
setFilter()  : RouteMap
Legacy method for backward compatibility
setFilters()  : RouteMap
Set filters for this route
setFunction()  : RouteMap
setName()  : RouteMap
Set the name for this route and register it with the router for duplicate detection.
setParameters()  : RouteMap
setPath()  : RouteMap
setRouterContext()  : RouteMap
Set the router and method for this route (used for name duplicate detection)
checkForDuplicateParams()  : void

Properties

$Filters

public array<string|int, mixed> $Filters

$Parameters

public array<string|int, mixed> $Parameters

$Payload

public array<string|int, mixed> $Payload

$_method

private string|null $_method = null

HTTP method for this route (GET, POST, etc.)

$_router

private Router|null $_router = null

Reference to router for name validation

Methods

__construct()

RouteMap constructor.

public __construct(mixed $path, mixed $function[, mixed $filters = '' ]) : mixed
Parameters
$path : mixed

string route path i.e. /part/new or /part/:id

$function : mixed

callable the function to call on a matching route.

$filters : mixed = ''

string|array the name(s) of the filter(s) to match with this route.

Tags
throws
Exception

execute()

public execute(Router $router) : mixed
Parameters
$router : Router
Tags
throws
Exception

getFilter()

Legacy method for backward compatibility

public getFilter() : string|null

Use getFilters() instead

Return values
string|null

getFilters()

Get filters for this route

public getFilters() : array<string|int, mixed>
Return values
array<string|int, mixed>

getFunction()

public getFunction() : callable
Return values
callable

getParameters()

public getParameters() : null
Return values
null

getPath()

public getPath() : string
Return values
string

parseParams()

Extracts the template array from the route definition.

public parseParams() : array<string|int, mixed>
Tags
throws
Exception
Return values
array<string|int, mixed>

setFilter()

Legacy method for backward compatibility

public setFilter(string $filter) : RouteMap

Use setFilters() instead

Parameters
$filter : string
Return values
RouteMap

setFilters()

Set filters for this route

public setFilters(string|array<string|int, mixed> $filters) : RouteMap
Parameters
$filters : string|array<string|int, mixed>
Return values
RouteMap

setFunction()

public setFunction(callable $function) : RouteMap
Parameters
$function : callable
Return values
RouteMap

setName()

Set the name for this route and register it with the router for duplicate detection.

public setName(string $name) : RouteMap
Parameters
$name : string
Tags
throws
DuplicateRouteException

If name is already in use

Return values
RouteMap

setParameters()

public setParameters(array<string|int, mixed> $parameters) : RouteMap
Parameters
$parameters : array<string|int, mixed>
Return values
RouteMap

setRouterContext()

Set the router and method for this route (used for name duplicate detection)

public setRouterContext(Router $router, string $method) : RouteMap
Parameters
$router : Router
$method : string

HTTP method (GET, POST, PUT, DELETE)

Return values
RouteMap

checkForDuplicateParams()

protected checkForDuplicateParams(mixed $param, mixed $params) : void
Parameters
$param : mixed
$params : mixed
Tags
throws
RouteParam

        
On this page

Search results