Teeny, a route system for PHP
Tags: php, php5, php7, php8
Published: 2021-02-03 01:57:02

Teeny is a micro-route system that is really micro, supports **PHP 5.3** to **PHP 8**, is extremely simple and ready to use.

For create your project use:

```
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 https://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
<?php
require_once 'vendor/teeny.php';

$app = new \Inphinit\Teeny;

...

return $app->exec();
```

For use without composer-autoload

``` php
<?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:

``` php
$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)](https://semver.org/spec/v2.0.0.html) format and `$params` returns `array( api => ...)`

For use a pattern in routes, set like this:

``` php
$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();
```