Teeny is a micro-route system that is really micro, supports PHP 5.3 to PHP 8, is extremely simple and ready to use.
Github: github.com/inphinit/teeny
Teeny ports:
To create a project, you can use the composer:
composer create-project inphinit/teeny <project name>
Replace <project name>
by your project name, for exemple, if want create your project with "blog" name (folder name), use:
composer create-project inphinit/teeny blog
Download without composer
If is not using composer
try direct download from github.com/inphinit/teeny/releases
Copy release or create project in Apache or Nginx folder and configure Vhost in Apache or execute direct from folder
For use with composer-auto configure index.php
like this:
<?php
require_once 'vendor/teeny.php';
$app = new \Inphinit\Teeny;
...
return $app->exec();
For use without composer-autoload
<?php
require_once 'vendor/teeny.php';
require_once 'vendor/autoload.php';
$app = new \Inphinit\Teeny;
...
return $app->exec();
Stand-alone server
For use without Apache or Nginx you can execute this command in folder:
php -S localhost:8080 index.php
Handling Http errors (like ErrorDocument)
For handling errors for not defined routes (404 Not Found) and when try access a route with invalid (not defined) method uses $app->handlerCodes(array $codes, mixed $callback)
, example (in routes.js), example:
$app->handlerCodes([ 403, 404, 405 ], function ($code) {
echo 'Custom page error ', $code;
});
Route patterns
You can create your own patterns to use with the routes in Teeny, but there are also ready-to-use patterns:
Type | Example | Description |
alnum | $app->action('GET', '/baz/<video:alnum>', ...); | Only accepts parameters with alpha-numeric format and $params returns array( video => ...) |
alpha | $app->action('GET', '/foo/bar/<name:alpha>', ...); | Only accepts parameters with alpha format and $params returns array( name => ...) |
decimal | $app->action('GET', '/baz/<price:decimal>', ...); | Only accepts parameters with decimal format and $params returns array( price => ...) |
num | $app->action('GET', '/foo/<id:num>', ...); | Only accepts parameters with integer format and $params returns array( id => ...) |
noslash | $app->action('GET', '/foo/<noslash:noslash>', ...); | Accpets any characters expcet slashs (/ ) |
nospace | $app->action('GET', '/foo/<nospace:nospace>', ...); | Accpets any characters expcet spaces, like white-spaces (%20 ), tabs (%0A ) and others (see about \S in regex) |
uuid | $app->action('GET', '/bar/<barcode:alnum>', ...); | Only accepts parameters with uuid format and $params returns array( barcode => ...) |
version | $app->action('GET', '/baz/<api:version>', ...); | Only accepts parameters with semversion (v2) format and $params returns array( api => ...) |
For use a pattern in routes, set like this:
$app->action('GET', '/user/<name:alnum>', function ($request, $response, $params) {
return "Hello {$params['name']}";
});
$app->action('GET', '/api/<foobar:version>', function ($request, $response, $params) {
return "Version: {$params['foobar']}";
});
$app->action('GET', '/product/<id:num>', function ($request, $response, $params) {
return "Product ID: {$params['id']}";
});
...
return $app->exec();