Skip to content
Snippets Groups Projects
Commit 8e8903ef authored by Hahn Axel (hahn)'s avatar Hahn Axel (hahn)
Browse files

update docs

parent 1c35c577
No related branches found
No related tags found
No related merge requests found
# 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/)
# 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');
```
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment