One small single file with Router, Request and Response classes for tiny PHP Apps.
TinyRoute provides:
- Registration of Post and Get routes.
mod_rewrite
and regex based route matching- An Http Request class that queries
$_POST
and$_GET
- An Http Response class that returns Json encoded replies.
You can simply download the TinyRoute.php file, put it somewhere on your disk and include it from your script.
include('TinyRoute.php`);
You can get the repository from Github: https://github.com/aoloe/php-tiny-rest...
... and then link it in your projects composer.json
by the path on your computer:
"repositories": [
{
"type": "path",
"url": "/your/path/to/php-tiny-route"
}
],
"require": {
"aoloe/tiny-route": "@dev"
}
See the test script below for a basic usage (and TinyRest cannot do much more than that...).
You can also tell Composer to get the TinyRoute from Github:
"repositories": [
{
"type": "vcs",
"url": "git@github.com:aoloe/php-tiny-route.git"
},
],
"require": {
"aoloe/tiny-route": "dev-master"
}
See the test script below for a basic usage (and TinyRoute cannot do much more than that...).
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
include('../vendor/autoload.php');
$router = new Aoloe\TinyRoute\Router();
$request = Aoloe\TinyRoute\HttpRequest::create();
$response = new Aoloe\TinyRoute\HttpResponse();
$router->get('/(\w+)', function($name) use($response) {
$response->respond('<html><body><p>Hello '.$name.'</body></html>');
});
if (!$router->run($request)) {
$response->error_404();
}
If you're using the php internal server, you can use this mod_rewrite router and test the script with:
http://localhost:8080/Arthur