PHP Classes

File: test/php/test3.php

Recommend this page to a friend!
  Packages of Nikos M.   Dromeo PHP Router Library   test/php/test3.php   Download  
File: test/php/test3.php
Role: Example script
Content type: text/plain
Description: Example script
Class: Dromeo PHP Router Library
Route HTTP requests to functions with URL patterns
Author: By
Last change: v.1.3.0 in progress

* new patterns ASCII, ANY suitable for ascii/non-ascii,unicode
v.1.3.0 in progress
v.1.3.0 in progress

* remove redirection code
* generix getter from original source, instead of passing alternative similar input
* edits, typos, code styling
Date: 6 months ago
Size: 1,675 bytes
 

Contents

Class file image Download
<?php

require(dirname(__FILE__) . '/../../src/php/Dromeo.php');

function
defaultHandler($route)
{
    echo(
'Default Handler' . PHP_EOL);
   
print_r($route);
}
function
get_from_($source)
{
    return function(
$key, $val, $start, $end, $input) use ($source) {
        return
substr($source, $start, $end-$start);
    };
}
$router1 = new Dromeo();
$router2 = new Dromeo();

$router1->on(array(
    array(
       
'route'=>'/foo/{:user}/{:id}',
       
'name'=> 'route1',
       
'handler'=> 'defaultHandler'
   
)
));
$router1->onGroup('/bar', function($router) {
   
$router->onGroup('/baz', function($router) {
           
$router->on([
               
'route'=> '/{:user}/{:id}',
               
'name'=> 'route4',
               
'handler'=> 'defaultHandler'
           
])
            ;
        })->
on([
           
'route'=> '/{:user}/{:id}',
           
'name'=> 'route3',
           
'handler'=> 'defaultHandler'
       
])
    ;
});
$router2->on(array(
    array(
       
'route'=>'/foo{/%ALPHA%-%ALPHA%:user(2)}',
       
'name'=> 'route2',
       
'handler'=> 'defaultHandler'
   
)
));

echo(
'Dromeo.VERSION = ' . Dromeo::VERSION . PHP_EOL);
echo(
PHP_EOL);

$router1->route(strtolower('/FOO/USER/ID'), '*', true, get_from_('/FOO/USER/ID'));
$router1->route(strtolower('/FOO/Foo/ID'), '*', true, get_from_('/FOO/Foo/ID'));
$router1->route(strtolower('/Bar/Foo/ID'), '*', true, get_from_('/Bar/Foo/ID'));
$router1->route(strtolower('/Bar/bAz/Foo/ID'), '*', true, get_from_('/Bar/bAz/Foo/ID'));
$router2->route(strtolower('/FOO/USER-User'), '*', true, get_from_('/FOO/USER-User'));
$router2->route(strtolower('/FOO/Foo-fOO'), '*', true, get_from_('/FOO/Foo-fOO'));