diff --git a/docs/20_Usage.md b/docs/20_Usage.md index 2059f1a6dcbb743a76294fc95bce2c579543e30d..bc85c5280e70a23ee88da912518e00a9309fa915 100644 --- a/docs/20_Usage.md +++ b/docs/20_Usage.md @@ -9,7 +9,7 @@ require_once('../classes/tinyrouter.class.php'); ### Config -Define the routes as array elements. +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. ```php @@ -23,8 +23,9 @@ $aRoutes=[ [ "/apps/@appid:[0-9a-f]*/@what:[a-z]*", "acess_appdata" ], // ^ ^ // | | -// route callback (string|array|... any type you want to get back) -// string = folder +// | 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 ]; @@ -62,9 +63,9 @@ The getRoute() method returns an array * request url * request method (GET|POST|PUT|DELETE|...) -* with the matching route -* name of the callback -* vars on parts with starting @ chars +* 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 ```txt // on url /apps/12345/meta @@ -86,12 +87,12 @@ Array If no route matches - or a variable did not match a required regex - then getRoute() returns *false*. -### Other getters +### Specific getters -Maybe the keys of the array change in future. You can access the data with specialized getter functions: +Maybe the keys of the array above change in future. You can access the data with specialized getter functions: ```php -// get the fallback +// get the callback item only $sAction=$oRouter->getCallback(); // all vars @@ -102,7 +103,7 @@ $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 +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". ```php @@ -121,4 +122,7 @@ To continue with the variables above: you maybe want to execute the method with ```php $aData=$Obj->$sAction($sAppId, $sWhat); +// ... +header('Content-Type: application/json'); +echo json_encode($aData); ```