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
Parameters
$app
mixed
An object passed to each route callback, defaults to a new App instance
Alias to set a response code, lock the response, and halt the route matching/dispatching
abort(int $code) : void
Parameters
$code
int
Optional HTTP status code to send
Exceptions
Returns the app object
app() : mixed
DELETE alias for "respond()"
delete(string $route, callable $callback) : callable
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
Parameters
$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
void
string
GET alias for "respond()"
get(string $route, callable $callback) : callable
Parameters
$route
string
$callback
callable
Returns
callable
Adds an error callback to the stack of error handlers
onError(callable $callback) : boolean | void
Parameters
$callback
callable
The callable function to execute in the error handling chain
Returns
boolean
void
POST alias for "respond()"
post(string $route, callable $callback) : callable
Parameters
$route
string
$callback
callable
Returns
callable
PUT alias for "respond()"
put(string $route, callable $callback) : callable
Parameters
$route
string
$callback
callable
Returns
callable
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!!!!';
});
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
Quick alias to skip the next callback/route method from executing
skipNext(int $num) : void
Parameters
$num
int
The number of next matches to skip
Exceptions
Quick alias to stop the remaining callbacks/route methods from executing
skipRemaining() : void
Quick alias to skip the current callback/route method from executing
skipThis() : void
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');
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
Parameters
$route
string
The route string to compile
Routes an exception through the error callbacks
error(\Exception $err) : void
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
Parameters
$callback
callable
$matched
int
$methods_matched
int
Properties
$errorCallbacks : \Klein\array[callable]
Constants
Dispatch route output handling
DISPATCH_CAPTURE_AND_APPEND
Capture all output and append it to the response body
Dispatch route output handling
DISPATCH_CAPTURE_AND_PREPEND
Capture all output and prepend it to the response body
Dispatch route output handling
DISPATCH_CAPTURE_AND_REPLACE
Capture all output and replace the response body with it
Dispatch route output handling
DISPATCH_CAPTURE_AND_RETURN
Capture all output and return it from dispatch
Dispatch route output handling
DISPATCH_NO_CAPTURE
Don't capture anything. Behave as normal.