obullo/router

A standalone and secure Router package developed for Obullo-Pages inspired by Django Url Dispatcher.

1.8.1 2020-07-06 21:39 UTC

This package is auto-updated.

Last update: 2020-07-14 17:04:16 UTC


README

Build Status License

A standalone and secure Router package developed for Obullo-Pages inspired by Django Url Dispatcher.

Install

$ composer require obullo/router

Requirements

The following versions of PHP are supported by this version.

  • 7.1
  • 7.2
  • 7.3
  • 7.4

Testing

$ vendor/bin/phpunit

Quick start

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

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

Psr7 Request

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

Route Collection

$collection = new RouteCollection($config);
$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

use Obullo\Router\RouteInterface;

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

if ($route instanceof RouteInterface) {
    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
}

Dispatch

$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 Laminas\Diactoros\Response\HtmlResponse;

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

YAML example

Basic .yaml configuration

## routes.yaml

/:
    handler: Views/default.phtml

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

Reading routes from .yaml files

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

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
};
$config = [
    'types' => [
        new IntType('<int:id>'),
        new StrType('<str:name>'),
        new SlugType('<slug:slug>'),
        new TranslationType('<locale:locale>'),
    ]
];
$request = Laminas\Diactoros\ServerRequestFactory::fromGlobals();
$context = new RequestContext;
$context->fromRequest($request);

$collection = new RouteCollection($config);
$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();
}