Documentation

Router
in package

Pop router class

Tags
category

Pop

author

Nick Sagona, III nick@popphp.org

copyright

Copyright (c) 2009-2026 Nick Sagona, III

license

https://www.popphp.org/license New BSD License

version
5.0.0

Table of Contents

Properties

$action  : mixed
Action
$dispatchable  : mixed
Dispatchable object
$dispatchableClass  : string|null
Dispatchable class
$dispatchableTraitsCache  : array<string, array<string|int, string>>
Cache of resolved trait names (own + inherited) per dispatchable class name.
$routeMatch  : MatchInterface|null
Route match object

Methods

__call()  : static
Magic method to register a route for a whitelisted custom HTTP method
__construct()  : mixed
Constructor
acceptsHtml()  : bool
Determine if the inbound request has a real preference for an HTML response
addCustomMethod()  : static
Add a custom HTTP method to the whitelist
addCustomMethods()  : static
Add multiple custom HTTP methods to the whitelist
addDispatchableParams()  : static
Add dispatchable params to be passed into a new dispatchable instance
addRoute()  : static
Add a route
addRoutes()  : static
Add multiple controller routes
appendDispatchableParams()  : static
Append dispatchable params to be passed into a new dispatchable instance
connect()  : static
Add a CONNECT route
delete()  : static
Add a DELETE route
get()  : static
Add a GET route
getAction()  : mixed
Get the action
getAllowedMethods()  : array<string|int, mixed>
Get the methods accepted by at least one path-matching route from the last route() call
getDispatchable()  : mixed
Get the current dispatchable object
getDispatchableClass()  : string
Get the current dispatchable class name
getDispatchableParams()  : mixed
Get the params assigned to the dispatchable
getRouteMatch()  : MatchInterface
Get route match object
getRouteParams()  : array<string|int, mixed>
Get the params discovered from the route
getRoutes()  : array<string|int, mixed>
Get routes
getUrl()  : string
Get URL for the named route
hasAction()  : bool
Determine if the router has an action
hasCustomMethod()  : bool
Determine if a custom HTTP method has been whitelisted
hasDispatchable()  : bool
Determine if the router has a dispatchable
hasDispatchableParams()  : bool
Determine if the dispatchable has params
hasMethodMismatch()  : bool
Determine if the last route() call matched a path whose method was rejected
hasName()  : bool
Has a route name
hasRoute()  : bool
Determine if there is a route match
hasRouteParams()  : bool
Determine if the route has params
head()  : static
Add a HEAD route
isCli()  : bool
Determine if the route is CLI
isHttp()  : bool
Determine if the route is HTTP
methodNotAllowed()  : void
Send a 405 Method Not Allowed response
name()  : static
Add a route name
noRouteFound()  : void
Method to process if a route was not found
options()  : static
Add an OPTIONS route
patch()  : static
Add a PATCH route
post()  : static
Add a POST route
prepare()  : static
Prepare routes
put()  : static
Add a PUT route
removeDispatchableParams()  : static
Remove dispatchable params
route()  : void
Route to the correct controller
trace()  : static
Add a TRACE route
httpMatch()  : Http
Get the active HTTP match object, guarding that the router is in HTTP mode

Properties

$action

Action

protected mixed $action = null

$dispatchable

Dispatchable object

protected mixed $dispatchable = null

$dispatchableClass

Dispatchable class

protected string|null $dispatchableClass = null

$dispatchableTraitsCache

Cache of resolved trait names (own + inherited) per dispatchable class name.

protected static array<string, array<string|int, string>> $dispatchableTraitsCache = []

A class's trait/inheritance shape never changes at runtime, so this is safe to share across all Router instances for the life of the process.

Methods

__call()

Magic method to register a route for a whitelisted custom HTTP method

public __call(string $name, array<string|int, mixed> $arguments) : static

Only forwards to the HTTP match object when $name is either a real method there or a whitelisted custom verb - not in HTTP mode at all, or in HTTP mode but neither of those, both mean $name isn't a real method on Router, so it's reported as such rather than misdiagnosed as an HTTP/CLI mode mismatch or an unregistered custom HTTP verb (the explicit HTTP-only proxy methods like get()/post()/addCustomMethod() are unaffected - they call httpMatch() directly and still correctly throw "not HTTP" when called against a CLI-mode router).

Parameters
$name : string
$arguments : array<string|int, mixed>
Tags
throws
Exception
Return values
static

__construct()

Constructor

public __construct([array<string|int, mixed>|null $routes = null ][, AbstractMatch|null $match = null ]) : mixed

Instantiate the router object

Parameters
$routes : array<string|int, mixed>|null = null
$match : AbstractMatch|null = null

acceptsHtml()

Determine if the inbound request has a real preference for an HTML response

public acceptsHtml() : bool
Tags
throws
Exception
Return values
bool

addCustomMethod()

Add a custom HTTP method to the whitelist

public addCustomMethod(string $method) : static
Parameters
$method : string
Tags
throws
Exception
Return values
static

addCustomMethods()

Add multiple custom HTTP methods to the whitelist

public addCustomMethods(array<string|int, mixed> $methods) : static
Parameters
$methods : array<string|int, mixed>
Tags
throws
Exception
Return values
static

addDispatchableParams()

Add dispatchable params to be passed into a new dispatchable instance

public addDispatchableParams(string $dispatchable, mixed $params) : static
Parameters
$dispatchable : string
$params : mixed
Return values
static

addRoute()

Add a route

public addRoute(string $route, mixed $controller) : static
Parameters
$route : string
$controller : mixed
Return values
static

addRoutes()

Add multiple controller routes

public addRoutes(array<string|int, mixed> $routes) : static
Parameters
$routes : array<string|int, mixed>
Return values
static

appendDispatchableParams()

Append dispatchable params to be passed into a new dispatchable instance

public appendDispatchableParams(string $dispatchable, mixed $params) : static
Parameters
$dispatchable : string
$params : mixed
Return values
static

connect()

Add a CONNECT route

public connect(string $route, mixed $controller) : static
Parameters
$route : string
$controller : mixed
Tags
throws
Exception
Return values
static

delete()

Add a DELETE route

public delete(string $route, mixed $controller) : static
Parameters
$route : string
$controller : mixed
Tags
throws
Exception
Return values
static

get()

Add a GET route

public get(string $route, mixed $controller) : static
Parameters
$route : string
$controller : mixed
Tags
throws
Exception
Return values
static

getAction()

Get the action

public getAction() : mixed

getAllowedMethods()

Get the methods accepted by at least one path-matching route from the last route() call

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

getDispatchable()

Get the current dispatchable object

public getDispatchable() : mixed

getDispatchableClass()

Get the current dispatchable class name

public getDispatchableClass() : string
Return values
string

getDispatchableParams()

Get the params assigned to the dispatchable

public getDispatchableParams(string $dispatchable) : mixed
Parameters
$dispatchable : string

getRouteParams()

Get the params discovered from the route

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

getRoutes()

Get routes

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

getUrl()

Get URL for the named route

public getUrl(string $routeName[, mixed $params = null ][, bool $fqdn = false ]) : string
Parameters
$routeName : string
$params : mixed = null
$fqdn : bool = false
Tags
throws
Exception
Return values
string

hasAction()

Determine if the router has an action

public hasAction() : bool
Return values
bool

hasCustomMethod()

Determine if a custom HTTP method has been whitelisted

public hasCustomMethod(string $method) : bool
Parameters
$method : string
Tags
throws
Exception
Return values
bool

hasDispatchable()

Determine if the router has a dispatchable

public hasDispatchable() : bool
Return values
bool

hasDispatchableParams()

Determine if the dispatchable has params

public hasDispatchableParams(string $dispatchable) : bool
Parameters
$dispatchable : string
Return values
bool

hasMethodMismatch()

Determine if the last route() call matched a path whose method was rejected

public hasMethodMismatch() : bool
Return values
bool

hasName()

Has a route name

public hasName(string $routeName) : bool
Parameters
$routeName : string
Return values
bool

hasRoute()

Determine if there is a route match

public hasRoute() : bool
Return values
bool

hasRouteParams()

Determine if the route has params

public hasRouteParams() : bool
Return values
bool

head()

Add a HEAD route

public head(string $route, mixed $controller) : static
Parameters
$route : string
$controller : mixed
Tags
throws
Exception
Return values
static

isCli()

Determine if the route is CLI

public isCli() : bool
Return values
bool

isHttp()

Determine if the route is HTTP

public isHttp() : bool
Tags
phpstan-assert-if-true

Match\Http $this->routeMatch

Return values
bool

methodNotAllowed()

Send a 405 Method Not Allowed response

public methodNotAllowed(array<string|int, mixed> $allowedMethods[, bool $exit = true ]) : void
Parameters
$allowedMethods : array<string|int, mixed>
$exit : bool = true
Tags
throws
Exception

name()

Add a route name

public name(string $routeName) : static
Parameters
$routeName : string
Return values
static

noRouteFound()

Method to process if a route was not found

public noRouteFound([bool $exit = true ]) : void
Parameters
$exit : bool = true

options()

Add an OPTIONS route

public options(string $route, mixed $controller) : static
Parameters
$route : string
$controller : mixed
Tags
throws
Exception
Return values
static

patch()

Add a PATCH route

public patch(string $route, mixed $controller) : static
Parameters
$route : string
$controller : mixed
Tags
throws
Exception
Return values
static

post()

Add a POST route

public post(string $route, mixed $controller) : static
Parameters
$route : string
$controller : mixed
Tags
throws
Exception
Return values
static

prepare()

Prepare routes

public prepare() : static
Return values
static

put()

Add a PUT route

public put(string $route, mixed $controller) : static
Parameters
$route : string
$controller : mixed
Tags
throws
Exception
Return values
static

removeDispatchableParams()

Remove dispatchable params

public removeDispatchableParams(string $dispatchable) : static
Parameters
$dispatchable : string
Return values
static

route()

Route to the correct controller

public route([string|array<string|int, mixed>|null $forceRoute = null ]) : void
Parameters
$forceRoute : string|array<string|int, mixed>|null = null
Tags
throws
Exception|ReflectionException

trace()

Add a TRACE route

public trace(string $route, mixed $controller) : static
Parameters
$route : string
$controller : mixed
Tags
throws
Exception
Return values
static

httpMatch()

Get the active HTTP match object, guarding that the router is in HTTP mode

protected httpMatch() : Http
Tags
throws
Exception
Return values
Http

        
On this page

Search results