Example

Load class

// load the class
require_once('../classes/tinyrouter.class.php');

Config

Define the routes and its return values as array elements. First item is a route. The 2nd item is something you get back as callback item of a matching route. Here are strings but you can define other variable types like objects here.

// define routes
$aRoutes=[
    [ "/config",                             "get_config"     ],
    [ "/config/@var",                        "get_config_var" ],

    [ "/apps",                               "listapps"       ],
    [ "/apps/@appid:[0-9a-f]*",              "acess_appdata"  ],
    [ "/apps/@appid:[0-9a-f]*/@what:[a-z]*", "acess_appdata"  ],
//    ^                                       ^
//    |                                       |
//    |                                       callback
//    route                                   a string|array|...
//      string     = folder                   any type you want to get back
//      @var       = set a variable
//      @var:regex = set variable if it matches the given regex
];

Initialize router

// take an url ... or use the request uri if you have pretty urls
$sApiUrl=isset($_GET['request']) && $_GET['request'] ? $_GET['request'] : false;

// init the class
$oRouter=new tinyrouter($aRoutes, $sApiUrl);

// it is the same like
// $oRouter=new tinyrouter();
// $oRouter->setRoutes($aRoutes);
// $oRouter->setUrl($sApiUrl);

Find route

// get the last matching route
$aFoundRoute=$oRouter->getRoute();
if(!$aFoundRoute){
    header('HTTP/1.0 400 Bad request');
    die('<h1>400 Bad request</h1>');
}

// ... continue

The getRoute() method returns an array

  • request url
  • request method (GET|POST|PUT|DELETE|…)
  • the detected best matching route
  • name of the callback (read from 2nd value in your config)
  • vars contains all variables coming from url parts starting with a @ char
// on url /apps/12345/meta

Array
(
    [request-method] => GET
    [request] => /apps/12345/meta
    [route] => /apps/@appid:[0-9a-f]*/@what:[a-z]*
    [callback] => acess_appdata
    [vars] => Array
        (
            [appid] => 12345
            [what] => meta
        )

)

If no route matches - or a variable did not match a required regex - then getRoute() returns false.

Specific getters

Maybe the keys of the array above change in future. You can access the data with specialized getter functions:

// get the callback item only
$sAction=$oRouter->getCallback();

// all vars
$aAllvars=$oRouter->getVars();

// get single vars
$sAppId=$oRouter->getVar('appid');
$sWhat=$oRouter->getVar('what');

You can get the nth element from the request url. In our example with index 0 you get the 1st element which can be “config” or “apps”.

// get 1st 
$sItem=isset($oRouter->getUrlParts()[0]) ? $oRouter->getUrlParts()[0] : false;

With those elements you get from the router you easily cam build your switch case blocks to execute the wanted method of your class.

Get data

The router is just a helper to map an url to something you can process.

To continue with the variables above: you maybe want to execute the method with the name $sAction and 2 params:

$aData=$Obj->$sAction($sAppId, $sWhat);
// ...
header('Content-Type: application/json');
echo json_encode($aData);