obullo/router

Php7 standalone router inspired by the Django Url Dispatcher. Easy to use and Secure.

1.4.1 2019-10-11 18:15 UTC

README

Build Status Software License Total Downloads

Obullo router is a standalone route package inspired by the Django Url Dispatcher.

Install

$ composer require obullo/router

Requirements

The following versions of PHP are supported by this version.

  • 7.0
  • 7.1
  • 7.2
  • 7.3

Testing

$ vendor/bin/phpunit

Quick start

require '../vendor/autoload.php';

use Obullo\Router\Pattern;
use Obullo\Router\Route;
use Obullo\Router\RequestContext;
use Obullo\Router\RouteCollection;
use Obullo\Router\Router;
use Obullo\Router\Types\{
    StrType,
    IntType
};
$pattern = new Pattern([
    new IntType('<int:id>'),
    new StrType('<str:name>'),
]);

Psr7 Request

$request = Zend\Diactoros\ServerRequestFactory::fromGlobals();
$context = new RequestContext;
$context->fromRequest($request);

Route Collection

$collection = new RouteCollection($pattern);
$collection->setContext($context);
$collection->add(new Route('GET', '/', 'Views/default.phtml'));
$collection->add(new Route('GET', '/dummy/index/<int:id>/<str:name>', 'Views/dummy.phtml'))->scheme(['http','https']);
$collection->add(new Route('GET', '/test/index', 'Views/test.phtml'))
    ->host('example.com');
    ->scheme('http');
    ->middleware(App\Middleware\Dummy::class);

Route Class

$route = $collection->get('/dummy/index/<int:id>/<str:name>');

echo $route->getHandler(); //  "App\Controller\DummyController::index"
echo $route->getMethods()[0]; // GET
echo $route->getPattern(); //  "/dummy/index/(?\d+)/(?\w+)/"
echo $route->getMiddlewares()[0]; // App\Middleware\Dummy::class

Dispatching

$router = new Router($collection);

if ($route = $router->matchRequest()) {
    $handler = $route->getHandler(); // Views/default.phtml
    $response = include $handler;

    if ($response instanceof Psr\Http\Message\ResponseInterface) {
        echo $response->getBody();
    }
}

dummy.phtml

// Views/dummy.phtml
// 
use Zend\Diactoros\Response\HtmlResponse;

return new HtmlResponse('Im a dummy view');

YAML example

Yaml basic

## routes.yaml

/:
    handler: Views/default.phtml

/<locale:locale>/dummy/<str:name>:
     handler: Views/dummy.phtml
     middleware: App\Middleware\Dummy

Parsing yaml

require '../vendor/autoload.php';

use Obullo\Router\Pattern;
use Obullo\Router\Route;
use Obullo\Router\RequestContext;
use Obullo\Router\RouteCollection;
use Obullo\Router\Router;
use Obullo\Router\Builder;
use Obullo\Router\Generator;
use Obullo\Router\Types\{
    StrType,
    IntType,
    SlugType,
    TranslationType
};
$pattern = new Pattern;
$pattern->add(new IntType('<int:id>'));
$pattern->add(new StrType('<str:name>'));
$pattern->add(new SlugType('<slug:slug>'));
$pattern->add(new TranslationType('<locale:locale>'));

$request = Zend\Diactoros\ServerRequestFactory::fromGlobals();
$context = new RequestContext;
$context->fromRequest($request);

$collection = new RouteCollection($pattern);
$collection->setContext($context);

use Symfony\Component\Yaml\Yaml;

$builder = new Builder($collection);
$collection = $builder->build(Yaml::parseFile('routes.yaml'));

$router = new Router($collection);

if ($route = $router->matchRequest()) {
    echo $handler = $route->getHandler();  // Views/default.phtml
    $methods = $route->getMethods();
}

Documentation

Documents are available at http://obullo.com/