Constants

ROUTE_COMPILE_REGEX

ROUTE_COMPILE_REGEX

The regular expression used to compile and match URL's

ROUTE_ESCAPE_REGEX

ROUTE_ESCAPE_REGEX

The regular expression used to escape the non-named param section of a route URL

DISPATCH_NO_CAPTURE

DISPATCH_NO_CAPTURE

Dispatch route output handling

Don't capture anything. Behave as normal.

DISPATCH_CAPTURE_AND_RETURN

DISPATCH_CAPTURE_AND_RETURN

Dispatch route output handling

Capture all output and return it from dispatch

DISPATCH_CAPTURE_AND_REPLACE

DISPATCH_CAPTURE_AND_REPLACE

Dispatch route output handling

Capture all output and replace the response body with it

DISPATCH_CAPTURE_AND_PREPEND

DISPATCH_CAPTURE_AND_PREPEND

Dispatch route output handling

Capture all output and prepend it to the response body

DISPATCH_CAPTURE_AND_APPEND

DISPATCH_CAPTURE_AND_APPEND

Dispatch route output handling

Capture all output and append it to the response body

Properties

$match_types

$match_types : 

The types to detect in a defined match "block"

Examples of these blocks are as follows:

  • integer: '[i:id]'
  • alphanumeric: '[a:username]'
  • hexadecimal: '[h:color]'
  • slug: '[s:article]'

Type

$routes

$routes : 

Collection of the routes to match on dispatch

Type

$route_factory

$route_factory : 

The Route factory object responsible for creating Route instances

Type

$error_callbacks

$error_callbacks : 

A stack of error callback callables

Type

$http_error_callbacks

$http_error_callbacks : 

A stack of HTTP error callback callables

Type

$after_filter_callbacks

$after_filter_callbacks : 

A queue of callbacks to call after processing the dispatch loop and before the response is sent

Type

$request

$request : 

The Request object passed to each matched route

Type

$response

$response : 

The Response object passed to each matched route

Type

$service

$service : 

The service provider object passed to each matched route

Type

$app

$app : 

A generic variable passed to each matched route

Type

$output_buffer_level

$output_buffer_level : 

The output buffer level used by the dispatch process

Type

Methods

__construct()

__construct(\app\framework\Component\Route\Klein\ServiceProvider  $service = null, mixed  $app = null, \app\framework\Component\Route\Klein\DataCollection\RouteCollection  $routes = null, \app\framework\Component\Route\Klein\AbstractRouteFactory  $route_factory = null) 

Constructor

Create a new Klein instance with optionally injected dependencies This DI allows for easy testing, object mocking, or class extension

Parameters

\app\framework\Component\Route\Klein\ServiceProvider $service

Service provider object responsible for utilitarian behaviors

mixed $app

An object passed to each route callback, defaults to an App instance

\app\framework\Component\Route\Klein\DataCollection\RouteCollection $routes

Collection object responsible for containing all route instances

\app\framework\Component\Route\Klein\AbstractRouteFactory $route_factory

A factory class responsible for creating Route instances

app()

app() : mixed

Returns the app object

Returns

mixed

respond()

respond(string|array  $method, string  $path = '*', callable  $callback = null) : \app\framework\Component\Route\Klein\Route

Add a new route to be matched on dispatch

Essentially, this method is a standard "Route" builder/factory, allowing a loose argument format and a standard way of creating Route instances

This method takes its arguments in a very loose format The only "required" parameter is the callback (which is very strange considering the argument definition order)

$router = new Klein();

$router->respond( function() { echo 'this works'; }); $router->respond( '/endpoint', function() { echo 'this also works'; }); $router->respond( 'POST', '/endpoint', function() { echo 'this also works!!!!'; });

Parameters

string|array $method

HTTP Method to match

string $path

Route URI path to match

callable $callback

Callable callback method to execute on route match

Returns

\app\framework\Component\Route\Klein\Route

with()

with(string  $namespace, callable|string  $routes) : void

Collect a set of routes under a common namespace

The routes may be passed in as either a callable (which holds the route definitions), or as a string of a filename, of which to "include" under the Klein router scope

$router = new Klein();

$router->with('/users', function($router) { $router->respond( '/', function() { // do something interesting }); $router->respond( '/[i:id]', function() { // do something different }); });

$router->with('/cars', DIR . '/routes/cars.php');

Parameters

string $namespace

The namespace under which to collect the routes

callable|string $routes

The defined routes callable or filename to collect under the namespace

dispatch()

dispatch(\app\framework\Component\Route\Klein\Request  $request = null, \app\framework\Component\Route\Klein\AbstractResponse  $response = null, boolean  $send_response = true, integer  $capture = self::DISPATCH_NO_CAPTURE) : void|string

Dispatch the request to the appropriate route(s)

Dispatch with optionally injected dependencies This DI allows for easy testing, object mocking, or class extension

Parameters

\app\framework\Component\Route\Klein\Request $request

The request object to give to each callback

\app\framework\Component\Route\Klein\AbstractResponse $response

The response object to give to each callback

boolean $send_response

Whether or not to "send" the response after the last route has been matched

integer $capture

Specify a DISPATCH_* constant to change the output capturing behavior

Returns

void|string

getPathFor()

getPathFor(string  $route_name, array  $params = null, boolean  $flatten_regex = true) : string

Get the path for a given route

This looks up the route by its passed name and returns the path/url for that route, with its URL params as placeholders unless you pass a valid key-value pair array of the placeholder params and their values

If a pathname is a complex/custom regular expression, this method will simply return the regular expression used to match the request pathname, unless an optional boolean is passed "flatten_regex" which will flatten the regular expression into a simple path string

This method, and its style of reverse-compilation, was originally inspired by a similar effort by Gilles Bouthenot (@gbouthenot)

Parameters

string $route_name

The name of the route

array $params

The array of placeholder fillers

boolean $flatten_regex

Optionally flatten custom regular expressions to "/"

Throws

\OutOfBoundsException

If the route requested doesn't exist

Returns

string

onError()

onError(callable  $callback) : void

Adds an error callback to the stack of error handlers

Parameters

callable $callback

The callable function to execute in the error handling chain

onHttpError()

onHttpError(callable  $callback) : void

Adds an HTTP error callback to the stack of HTTP error handlers

Parameters

callable $callback

The callable function to execute in the error handling chain

afterDispatch()

afterDispatch(callable  $callback) : void

Adds a callback to the stack of handlers to run after the dispatch loop has handled all of the route callbacks and before the response is sent

Parameters

callable $callback

The callable function to execute in the after route chain

skipThis()

skipThis() : void

Quick alias to skip the current callback/route method from executing

Throws

\app\framework\Component\Route\Klein\Exceptions\DispatchHaltedException

To halt/skip the current dispatch loop

skipNext()

skipNext(integer  $num = 1) : void

Quick alias to skip the next callback/route method from executing

Parameters

integer $num

The number of next matches to skip

Throws

\app\framework\Component\Route\Klein\Exceptions\DispatchHaltedException

To halt/skip the current dispatch loop

skipRemaining()

skipRemaining() : void

Quick alias to stop the remaining callbacks/route methods from executing

Throws

\app\framework\Component\Route\Klein\Exceptions\DispatchHaltedException

To halt/skip the current dispatch loop

abort()

abort(integer  $code = null) : void

Alias to set a response code, lock the response, and halt the route matching/dispatching

Parameters

integer $code

Optional HTTP status code to send

Throws

\app\framework\Component\Route\Klein\Exceptions\DispatchHaltedException

To halt/skip the current dispatch loop

options()

options(string  $path = '*', callable  $callback = null) : \app\framework\Component\Route\Klein\Route

OPTIONS alias for "respond()"

Parameters

string $path
callable $callback

Returns

\app\framework\Component\Route\Klein\Route

head()

head(string  $path = '*', callable  $callback = null) : \app\framework\Component\Route\Klein\Route

HEAD alias for "respond()"

Parameters

string $path
callable $callback

Returns

\app\framework\Component\Route\Klein\Route

get()

get(string  $path = '*', callable  $callback = null) : \app\framework\Component\Route\Klein\Route

GET alias for "respond()"

Parameters

string $path
callable $callback

Returns

\app\framework\Component\Route\Klein\Route

post()

post(string  $path = '*', callable  $callback = null) : \app\framework\Component\Route\Klein\Route

POST alias for "respond()"

Parameters

string $path
callable $callback

Returns

\app\framework\Component\Route\Klein\Route

put()

put(string  $path = '*', callable  $callback = null) : \app\framework\Component\Route\Klein\Route

PUT alias for "respond()"

Parameters

string $path
callable $callback

Returns

\app\framework\Component\Route\Klein\Route

delete()

delete(string  $path = '*', callable  $callback = null) : \app\framework\Component\Route\Klein\Route

DELETE alias for "respond()"

Parameters

string $path
callable $callback

Returns

\app\framework\Component\Route\Klein\Route

patch()

patch(string  $path = '*', callable  $callback = null) : \app\framework\Component\Route\Klein\Route

PATCH alias for "respond()"

PATCH was added to HTTP/1.1 in RFC5789

Parameters

string $path
callable $callback

Returns

\app\framework\Component\Route\Klein\Route

parseLooseArgumentOrder()

parseLooseArgumentOrder(mixed  $args) : array

Parse our extremely loose argument order of our "respond" method and its aliases

This method takes its arguments in a loose format and order. The method signature is simply there for documentation purposes, but allows for the minimum of a callback to be passed in its current configuration.

Parameters

mixed $args

An argument array. Hint: This works well when passing "func_get_args()" @named string | array $method HTTP Method to match @named string $path Route URI path to match @named callable $callback Callable callback method to execute on route match

Returns

array —

A named parameter array containing the keys: 'method', 'path', and 'callback'

compileRoute()

compileRoute(string  $route) : string

Compiles a route string to a regular expression

Parameters

string $route

The route string to compile

Returns

string

error()

error(\Exception|\Throwable  $err) : void

Routes an exception through the error callbacks

TODO: Change the $err parameter to type-hint against Throwable once PHP 5.x support is no longer necessary.

Parameters

\Exception|\Throwable $err

The exception that occurred

Throws

\app\framework\Component\Route\Klein\Exceptions\UnhandledException

If the error/exception isn't handled by an error callback

httpError()

httpError(\app\framework\Component\Route\Klein\Exceptions\HttpExceptionInterface  $http_exception, \app\framework\Component\Route\Klein\DataCollection\RouteCollection  $matched, array  $methods_matched) : void

Handles an HTTP error exception through our HTTP error callbacks

Parameters

\app\framework\Component\Route\Klein\Exceptions\HttpExceptionInterface $http_exception

The exception that occurred

\app\framework\Component\Route\Klein\DataCollection\RouteCollection $matched

The collection of routes that were matched in dispatch

array $methods_matched

The HTTP methods that were matched in dispatch

callAfterDispatchCallbacks()

callAfterDispatchCallbacks() : void

Runs through and executes the after dispatch callbacks

validateRegularExpression()

validateRegularExpression(string  $regex) : boolean

Validate a regular expression

This simply checks if the regular expression is able to be compiled and converts any warnings or notices in the compilation to an exception

Parameters

string $regex

The regular expression to validate

Throws

\app\framework\Component\Route\Klein\Exceptions\RegularExpressionCompilationException

If the expression can't be compiled

Returns

boolean