ddrv/router

simple routing with multidomain support

1.0.1 2018-08-18 09:58 UTC

README

Install

php composer.phar require ddrv/router

Usage

Routing


use Ddrv\Router\Group;
use Ddrv\Router\Router;
use Ddrv\Router\Rule;

$host = Group::hostGroup('api.example.com');

$prefix = Group::prefixGroup('api/v1');

$router = new Router('not-found');

$router->addMask('user', '[a-z]+');
$router->setMiddleware(array('api'));

$rest = Group::rest('/users', 'UserController:{*}', 'id:user', 'api.v1.user.{*}')
    ->setMiddleware(array('rest'))
    ->setNamespace('Controllers\Api\V1')
    ->addRule(
       Rule::post('/users/{id:user}/admin', 'UserController:admin')->setMiddleware(array('admin')),
       'api.v1.user.admin'
    )
;

$prefix->addGroup($rest);
$host->addGroup($prefix);
$router->addGroup($host);


$dispatch = $router->dispatch('http://api.example.com/api/v1/users/ddrv/admin', 'POST');

print_r(array(
    'isFound' => $dispatch->isFound(),
    'handler' => $dispatch->getHandler(),
    'arguments' => $dispatch->getArguments(),
    'middleware' => $dispatch->getMiddleware(),

));
Array
(
    [isFound] => true
    [handler] => Controllers\Api\V1\UserController:admin
    [arguments] => Array
        (
            [id] => ddrv
        )
    [middleware] => Array
        (
            [0] => api
            [1] => rest
            [2] => admin 
            
        )
)

Naming


$router = new Router('not-found', 'example.com', 'https');
$router->addRule(Rule::get('/user/{login}', 'handler'), 'profile.main');
$uri = $router->createUri('profile.main', array('login' => 'ddrv'));
print_r($uri);
https://example.com/user/ddrv