Klein

Main Klein router class

package Klein

 Methods

Constructor

__construct(\Klein\ServiceProvider $service, mixed $app, \Klein\DataCollection\RouteCollection $routes) 

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

access public

Parameters

$service

\Klein\ServiceProvider

Service provider object responsible for utilitarian behaviors

$app

mixed

An object passed to each route callback, defaults to a new App instance

$routes

\Klein\DataCollection\RouteCollection

Collection object responsible for containing all of the route instances

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

abort(int $code) : void
access public

Parameters

$code

int

Optional HTTP status code to send

Exceptions

\Klein\Exceptions\DispatchHaltedException To halt/skip the current dispatch loop

Returns the app object

app() : mixed
access public

Returns

mixed

DELETE alias for "respond()"

delete(string $route, callable $callback) : callable
access public

Parameters

$route

string

$callback

callable

Returns

callable

Dispatch the request to the approriate route(s)

dispatch(\Klein\Request $request, \Klein\Response $response, boolean $send_response, int $capture) : void | string

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

access public

Parameters

$request

\Klein\Request

The request object to give to each callback

$response

\Klein\Response

The response object to give to each callback

$send_response

boolean

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

$capture

int

Specify a DISPATCH_* constant to change the output capturing behavior

Returns

voidstring

GET alias for "respond()"

get(string $route, callable $callback) : callable
access public

Parameters

$route

string

$callback

callable

Returns

callable

Adds an error callback to the stack of error handlers

onError(callable $callback) : boolean | void
access public

Parameters

$callback

callable

The callable function to execute in the error handling chain

Returns

booleanvoid

POST alias for "respond()"

post(string $route, callable $callback) : callable
access public

Parameters

$route

string

$callback

callable

Returns

callable

PUT alias for "respond()"

put(string $route, callable $callback) : callable
access public

Parameters

$route

string

$callback

callable

Returns

callable

Returns the request object

request() : \Klein\Request
access public

Returns

Add a new route to be matched on dispatch

respond(string $method, string $path, callable $callback) : callable

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!!!!';
});
access public

Parameters

$method

string

| array $method HTTP Method to match

$path

string

Route URI path to match

$callback

callable

Callable callback method to execute on route match

Returns

callable$callback

Returns the response object

response() : \Klein\Response
access public

Returns

Returns the routes object

routes() : \Klein\DataCollection\RouteCollection

Returns the service object

service() : \Klein\ServiceProvider
access public

Returns

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

skipNext(int $num) : void
access public

Parameters

$num

int

The number of next matches to skip

Exceptions

\Klein\Exceptions\DispatchHaltedException To halt/skip the current dispatch loop

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

skipRemaining() : void
access public

Exceptions

\Klein\Exceptions\DispatchHaltedException To halt/skip the current dispatch loop

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

skipThis() : void
access public

Exceptions

\Klein\Exceptions\DispatchHaltedException To halt/skip the current dispatch loop

Collect a set of routes under a common namespace

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

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() use ( $router) {
    $router->respond( '/', function() {
        // do something interesting
    });
    $router->respond( '/[i:id]', function() {
        // do something different
    });
});

$router->with('/cars', __DIR__ . '/routes/cars.php');
access public

Parameters

$namespace

string

The namespace under which to collect the routes

$routes

callable

| string[filename] $routes The defined routes to collect under the namespace

Compiles a route string to a regular expression

compileRoute(string $route) : void
access protected

Parameters

$route

string

The route string to compile

Routes an exception through the error callbacks

error(\Exception $err) : void
access protected

Parameters

$err

\Exception

The exception that occurred

Exceptions

\Klein\Exceptions\UnhandledException If the error/exception isn't handled by an error callback

Handle a response callback

handleResponseCallback(callable $callback, int $matched, int $methods_matched) : void

This handles common exceptions and their output to keep the "dispatch()" method DRY

access protected

Parameters

$callback

callable

$matched

int

$methods_matched

int

 Properties

 

$app : mixed
access protected
 

$errorCallbacks : \Klein\array[callable]
access protected
 

$namespace : string
access protected
 

$request : \Klein\Request
access protected
 

$response : \Klein\Response
access protected
 

$routes : \Klein\DataCollection\RouteCollection
access protected
 

$service : \Klein\ServiceProvider
access protected

 Constants

 

Dispatch route output handling

DISPATCH_CAPTURE_AND_APPEND 

Capture all output and append it to the response body

const int
 

Dispatch route output handling

DISPATCH_CAPTURE_AND_PREPEND 

Capture all output and prepend it to the response body

const int
 

Dispatch route output handling

DISPATCH_CAPTURE_AND_REPLACE 

Capture all output and replace the response body with it

const int
 

Dispatch route output handling

DISPATCH_CAPTURE_AND_RETURN 

Capture all output and return it from dispatch

const int
 

Dispatch route output handling

DISPATCH_NO_CAPTURE 

Don't capture anything. Behave as normal.

const int