PHP Classes

File: test/js/test2.js

Recommend this page to a friend!
  Classes of Nikos M.  >  Dromeo PHP Router Library  >  test/js/test2.js  >  Download  
File: test/js/test2.js
Role: Auxiliary data
Content type: text/plain
Description: Auxiliary data
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 month ago
Size: 1,802 bytes
 

Contents

Class file image Download
"use strict";

var path = require('path'),
    Dromeo = require(path.join(__dirname, '../../src/js/Dromeo.js')),
    echo = console.log, stringify = JSON.stringify
;

function defaultHandler()
{
}

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

router.on([
    {
        route:'/{:user}/{:id}',
        name: 'route1',
        handler: defaultHandler
    },
    {
        route:'/{:user}{/%INT%:?id(1)}',
        name: 'route2',
        handler: defaultHandler
    },
    {
        route:'/{:user}{/%INT%:?id(1)}{/%ALPHA%:?action(1)}',
        name: 'route4',
        handler: defaultHandler
    },
    {
        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, strict)
{
    var out;
    try {
        out = router.make(route, params, strict);
    } catch( err ) {
        out = err.message;
    }
    return out;
}

echo('Dromeo.VERSION = ' + Dromeo.VERSION);
echo();

echo(make('route1', {user:'foo',id:'123'}));
echo(make('route1', {user:'foo',id:'123'}, true));
echo(make('route1', {user:'foo'}));
echo(make('route2', {user:'foo'}));
echo(make('route2', {user:'foo',id:'123'}));
echo(make('route2', {user:'foo',id:'123'}, true));
echo(make('route3', {user:'foo',id:'123'}));
echo(make('route4', {user:'foo'}));
echo(make('route4', {user:'foo',id:'123',action:'test'}));
echo(make('route4', {user:'foo',action:'test'}, true));
echo(make('route5', {user:'user'}));
echo(make('route6', {user:'user'}));
For more information send a message to info at phpclasses dot org.