ROUTE_COMPILE_REGEX
ROUTE_COMPILE_REGEX
The regular expression used to compile and match URL's
Klein
Main Klein router class
__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
\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 |
routes() : \app\framework\Component\Route\Klein\DataCollection\RouteCollection
Returns the routes object
request() : \app\framework\Component\Route\Klein\Request
Returns the request object
response() : \app\framework\Component\Route\Klein\Response
Returns the response object
service() : \app\framework\Component\Route\Klein\ServiceProvider
Returns the service object
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!!!!'; });
string|array | $method | HTTP Method to match |
string | $path | Route URI path to match |
callable | $callback | Callable callback method to execute on route match |
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');
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(\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
\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 |
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)
string | $route_name | The name of the route |
array | $params | The array of placeholder fillers |
boolean | $flatten_regex | Optionally flatten custom regular expressions to "/" |
If the route requested doesn't exist
abort(integer $code = null) : void
Alias to set a response code, lock the response, and halt the route matching/dispatching
integer | $code | Optional HTTP status code to send |
To halt/skip the current dispatch loop
options(string $path = '*', callable $callback = null) : \app\framework\Component\Route\Klein\Route
OPTIONS alias for "respond()"
string | $path | |
callable | $callback |
head(string $path = '*', callable $callback = null) : \app\framework\Component\Route\Klein\Route
HEAD alias for "respond()"
string | $path | |
callable | $callback |
get(string $path = '*', callable $callback = null) : \app\framework\Component\Route\Klein\Route
GET alias for "respond()"
string | $path | |
callable | $callback |
post(string $path = '*', callable $callback = null) : \app\framework\Component\Route\Klein\Route
POST alias for "respond()"
string | $path | |
callable | $callback |
put(string $path = '*', callable $callback = null) : \app\framework\Component\Route\Klein\Route
PUT alias for "respond()"
string | $path | |
callable | $callback |
delete(string $path = '*', callable $callback = null) : \app\framework\Component\Route\Klein\Route
DELETE alias for "respond()"
string | $path | |
callable | $callback |
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
string | $path | |
callable | $callback |
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.
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 |
A named parameter array containing the keys: 'method', 'path', and 'callback'
handleRouteCallback(\app\framework\Component\Route\Klein\Route $route, \app\framework\Component\Route\Klein\DataCollection\RouteCollection $matched, array $methods_matched) : void
Handle a route's callback
This handles common exceptions and their output to keep the "dispatch()" method DRY
\app\framework\Component\Route\Klein\Route | $route | |
\app\framework\Component\Route\Klein\DataCollection\RouteCollection | $matched | |
array | $methods_matched |
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.
\Exception|\Throwable | $err | The exception that occurred |
If the error/exception isn't handled by an error callback
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
\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 |
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
string | $regex | The regular expression to validate |
If the expression can't be compiled