Routes a request to specific endpoint and extracts variables.
A route must begin with '/'.
The order of the parameters of the route and the parameters of the routeFunction should be the same.
The order of routes must be in a order like:
- /recipe/
- /recipe/create (e.g. default data and id as response)
- /recipe/create/<name> (specific name for creation and id as response)
- /recipe/<id>
- /recipe/<id>/export
- /recipe/<id>/<someVar>
<?php
/**
* api.php
*
* @version 0.1
*
*/
require_once "RestRecipes/autoload.php";
use RestRecipes\Router;
$app = new Router();
// Index
$app->route("/recipe/", "GET", function() {
});
// Detail
$app->route("/recipe/<id>", "GET", function($id) {
});
// Modify Object
$app->route("/recipe/<id>", "PUT", function($id, $data) {
});
try {
$app->run();
} catch (RuntimeException $e) {
// Show some information that no API endpoint was called
}
The default querySelector of the Router class is $q
. You can call the api
like this:
GET api.php?q=/recipe/
GET api.php?q=/recipe/3
The AuthenticationProvider allows to check certain security options.
<?php
/**
* api.php
*
* @version 0.1
*
*/
require_once "RestRecipes/autoload.php";
use RestRecipes\Router;
use RestRecipes\AuthenticationProvider;
$security = new AuthenticationProvider(AuthenticationProvider::AUTH_HTTP_BASIC);
$app = new Router();
// Secure API route
$app->route("/secure/", "GET", function() {
$security->setHttpBasicAuthenticationCredentials("user", "password");
if (!$security->authenticate())
return false;
});
// Index
$app->route("/recipe/", "GET", function() {
});
// Detail
$app->route("/recipe/<id>", "GET", function($id) {
});
try {
$app->run();
} catch (RuntimeException $e) {
// Show some information that no API endpoint was called
}