PHP Classes

File: test/php/test2.php

Recommend this page to a friend!
  Classes of Nikos M.   Dromeo PHP Router Library   test/php/test2.php   Download  
File: test/php/test2.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.2.0, contd

* namedroutes are passed on to top router
* .off() is handled recursively in subrouters
* various edits and changes
Date: 1 year ago
Size: 2,037 bytes
 

Contents

Class file image Download
<?php

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

function
echo_($s)
{
    echo (
$s . PHP_EOL);
}

function
defaultHandler()
{
}

$router = new Dromeo('https://example.com');

$router->on(array(
    array(
       
'route'=>'/{:user}/{:id}',
       
'name'=> 'route1',
       
'handler'=> 'defaultHandler'
   
),
    array(
       
'route'=>'/{:user}{/%INT%:?id(1)}',
       
'name'=> 'route2',
       
'handler'=> 'defaultHandler'
   
),
    array(
       
'route'=>'/{:user}{/%INT%:?id(1)}{/%ALPHA%:?action(1)}',
       
'name'=> 'route4',
       
'handler'=> 'defaultHandler'
   
),
    array(
       
'route'=>'/bar/456',
       
'name'=> 'route3',
       
'handler'=> 'defaultHandler'
   
)
))
->
onGroup('/foo', function($router) {
   
$router->onGroup('/bar', function($router) {
       
$router->on([
           
'route'=>'/{:user}',
           
'name'=> 'route5',
           
'handler'=> 'defaultHandler'
       
]);
    })
    ->
on([
       
'route'=>'/{:user}',
       
'name'=> 'route6',
       
'handler'=> 'defaultHandler'
   
]);
});

function
make($route, $params=array(), $strict=false)
{
    global
$router;
    try {
       
$out = $router->make($route, $params, $strict);
    } catch( \
Exception $err ) {
       
$out = $err->getMessage();
    }
    return
$out;
}

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

echo_(make('route1', array('user'=>'foo','id'=>'123')));
echo_(make('route1', array('user'=>'foo','id'=>'123'), true));
echo_(make('route1', array('user'=>'foo')));
echo_(make('route2', array('user'=>'foo')));
echo_(make('route2', array('user'=>'foo','id'=>'123')));
echo_(make('route2', array('user'=>'foo','id'=>'123'), true));
echo_(make('route3', array('user'=>'foo','id'=>'123')));
echo_(make('route4', array('user'=>'foo')));
echo_(make('route4', array('user'=>'foo','id'=>'123','action'=>'test')));
echo_(make('route4', array('user'=>'foo','action'=>'test'), true));
echo_(make('route5', array('user'=>'user')));
echo_(make('route6', array('user'=>'user')));