Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found
Select Git revision
  • master
1 result

Target

Select target project
  • iml-open-source/tinyrouter-php-class
1 result
Select Git revision
  • master
1 result
Show changes
Commits on Source (3)
......@@ -8,4 +8,4 @@ Institute for Medical Education; University of Bern
📄 Source: <https://git-repo.iml.unibe.ch/iml-open-source/tinyrouter-php-class> \
📜 License: GNU GPL 3.0 \
📖 Docs: <https://os-docs.iml.unibe.ch/tinyrouter-php-class/>
📗 Docs: <https://os-docs.iml.unibe.ch/tinyrouter-php-class/>
## Example
## Config
### Load class
```php
// load the class
require_once('../classes/tinyrouter.class.php');
```
### Config
Define the routes 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
// define routes
$aRoutes=[
[ "/config", "get_config" ],
......@@ -23,7 +28,11 @@ $aRoutes=[
// @var = set a variable
// @var:regex = set variable if it matches the given regex
];
```
### Initialize router
```php
// take an url ... or use the request uri if you have pretty urls
$sApiUrl=isset($_GET['request']) && $_GET['request'] ? $_GET['request'] : false;
......@@ -34,7 +43,11 @@ $oRouter=new tinyrouter($aRoutes, $sApiUrl);
// $oRouter=new tinyrouter();
// $oRouter->setRoutes($aRoutes);
// $oRouter->setUrl($sApiUrl);
```
### Find route
```php
// get the last matching route
$aFoundRoute=$oRouter->getRoute();
if(!$aFoundRoute){
......@@ -73,6 +86,8 @@ Array
If no route matches - or a variable did not match a required regex - then getRoute() returns *false*.
### Other getters
Maybe the keys of the array change in future. You can access the data with specialized getter functions:
```php
......@@ -97,3 +112,13 @@ $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:
```php
$aData=$Obj->$sAction($sAppId, $sWhat);
```
# tinyrouter.class.php
<!-- BEGIN DOC-COMMENT H2 ../src/tinyrouter.class.php -->
## namespace iml
Axels first router
--------------------------------------------------------------------------------<br> <br> THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE <br> LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR <br> OTHER PARTIES PROVIDE THE PROGRAM ?AS IS? WITHOUT WARRANTY OF ANY KIND, <br> EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED <br> WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE <br> ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. <br> SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY <br> SERVICING, REPAIR OR CORRECTION.<br> <br> --------------------------------------------------------------------------------<br> *
## public function __construct($aRoutes=[],$sUrl=false)
constructor
**Parameters:**
Var | Type | Desciption
-- |-- |--
array | $aRoutes | array of routes
string | $sUrl | incoming url
**Return:**
boolean
## protected function _getRoute()
detect last matching route item if no route matches then it returns false
**Return:**
array
## public function setRoutes($aRoutes=[])
set routes
**Parameters:**
Var | Type | Desciption
-- |-- |--
$aRoutes | array | list of [ route, more params ... ]
**Return:**
boolean
## public function setUrl($sUrl)
set incoming url, add the request behind protocol and domain.
**Parameters:**
Var | Type | Desciption
-- |-- |--
sUrl | string | url to fetch; /api/v1/productbyid/3424084
**Return:**
boolean
## public function getUrlParts($sUrl=false)
helper function: get url request parts as array
**Parameters:**
Var | Type | Desciption
-- |-- |--
$sUrl | string | url to handle; /api/v1/productbyid/3424084
**Return:**
array
## public function getRoute()
detect last matching route item if no route matches then it returns false
**Return:**
array|bool
## public function getCallback()
return the callback iten of the matching route If no route was matching it returns false
**Return:**
{*}
## public function getVars()
return the variables as keys in route parts with starting @ character
**Return:**
array
## public function getVar($sVarname)
return the variables as keys in route parts with starting @ character
**Return:**
string
## public function getSubitems()
get an array with next level route entries releative to the current route
**Return:**
array
<!-- END DOC-COMMENT -->
\ No newline at end of file
......@@ -10,4 +10,4 @@ Institute for Medical Education; University of Bern
📄 Source: <https://git-repo.iml.unibe.ch/iml-open-source/tinyrouter-php-class> \
📜 License: GNU GPL 3.0 \
📖 Docs: <https://os-docs.iml.unibe.ch/tinyrouter-php-class/>
📗 Docs: <https://os-docs.iml.unibe.ch/tinyrouter-php-class/>
#!/bin/bash
cd "$( dirname "$0")" || exit 1
docblox2md -v 30_Methods.md
......@@ -15,7 +15,7 @@
* SERVICING, REPAIR OR CORRECTION.<br>
* <br>
* --------------------------------------------------------------------------------<br>
* @version 1.0
* @version 1.1
* @author Axel Hahn
* @link https://github.com/iml-it/appmonitor
* @license GPL
......@@ -34,12 +34,14 @@ class tinyrouter{
/**
* constructor
* @param $aRoutes array array of routings
* @param $aRoutes array array of routes
* @param $sUrl string incoming url
* @return boolean
*/
public function __construct($aRoutes=[],$sUrl=false){
$this->setRoutes($aRoutes);
$this->setUrl($sUrl);
return true;
}
// ----------------------------------------------------------------------
......@@ -49,6 +51,7 @@ class tinyrouter{
/**
* detect last matching route item
* if no route matches then it returns false
* @return array
*/
protected function _getRoute(){
$aReturn=[];
......@@ -124,8 +127,9 @@ class tinyrouter{
}
/**
* set incoming url
* @param string sUrl url to fetch; https://api.exaple.com/
* set incoming url, add the request behind protocol and domain.
* @param string sUrl url to fetch; /api/v1/productbyid/3424084
* @return boolean
*/
public function setUrl($sUrl){
$this->sUrl=$sUrl;
......@@ -138,7 +142,8 @@ class tinyrouter{
// ----------------------------------------------------------------------
/**
* helper function: get url parts as array
* helper function: get url request parts as array
* @param string $sUrl url to handle; /api/v1/productbyid/3424084
* @returns array
*/
public function getUrlParts($sUrl=false){
......@@ -163,8 +168,9 @@ class tinyrouter{
}
/**
* return the callback of matching route
* @return string
* return the callback iten of the matching route
* If no route was matching it returns false
* @return {*}
*/
public function getCallback(){
return isset($this->aMatch['callback']) ? $this->aMatch['callback'] : false;
......