From 8e8903ef13dd2cae3b005c2ca651032dae497fcc Mon Sep 17 00:00:00 2001 From: "Hahn Axel (hahn)" <axel.hahn@iml.unibe.ch> Date: Mon, 20 Jun 2022 18:34:26 +0200 Subject: [PATCH] update docs --- README.md | 10 +++--- docs/_index.md | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+), 6 deletions(-) create mode 100644 docs/_index.md diff --git a/README.md b/README.md index 13b24f7..343b0ea 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,10 @@ -# ldap.class.php +# TinyRouter -A PHP class that I use - -* for authentication of user logins -* CRUD actions on ldap nodes +Axels first php class of a router. Institute for Medical Education; University of Bern +Source: <https://git-repo.iml.unibe.ch/iml-open-source/tinyrouter-php-class/> License: GNU GPL 3 -see [docs](docs/) \ No newline at end of file +see [docs](docs/) diff --git a/docs/_index.md b/docs/_index.md new file mode 100644 index 0000000..d01e217 --- /dev/null +++ b/docs/_index.md @@ -0,0 +1,89 @@ +# TinyRouter + +Axels first php class of a router. + +## Installation + +Copy the file *tinyrouter.class.php* somewhere into your project. + +## Example + +```php + +// load the class +require_once('../classes/tinyrouter.class.php'); + +// 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" ], +// ^ ^ +// | | +// route callback (string|array|... any type you want to get back) +// string = folder +// @var = set a variable +// @var:regex = set variable if it matches the given regex +]; + +// 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); + +// 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 + +* with the matching route +* name of the callback +* vars on parts with starting @ chars + +```txt +// on url /apps/12345/meta + +Array +( + [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*. + +Maybe the keys of the array change in future. You can access the data with specialized getter functions: + +```php +// get the fallback +$sAction=$oRouter->getCallback(); + +// all vars +$aAllvars=$oRouter->getVars(); + +// get single vars +$sAppId=$oRouter->getVar('appid'); +$sWhat=$oRouter->getVar('what'); +``` -- GitLab