2017 © Pedro Peláez
 

library sfw-router

A simple Router that maps incoming requests to predefined handlers

image

pwm/sfw-router

A simple Router that maps incoming requests to predefined handlers

  • Tuesday, October 31, 2017
  • by pwm
  • Repository
  • 2 Watchers
  • 0 Stars
  • 2,594 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 5 Versions
  • 19 % Grown

The README.md

SFW Router

Build Status codecov Maintainability Test Coverage License: MIT, (*1)

A simple Router that maps incoming requests to predefined handlers., (*2)

It builds a tree using segments of a predefined uri as internal nodes and its corresponding handler as the terminal node. It resolves routes by traversing this tree., (*3)

Wildcard segments are supported and captured for use in the handler. See usage for more detail., (*4)

Exact segment match wins over wildcard match. Eg. if you have /foo/bar and /foo/{x} defined as routes with corresponding handlers Bar and X then /foo/bar will be handled by Bar while /foo/baz will be handled by X., (*5)

Table of Contents

Requirements

PHP 7.1+, (*6)

Installation

composer require pwm/sfw-router

Usage

// Router depends on Request
use SFW\Request\Request;
use SFW\Request\RequestMethod as Method;
use SFW\Request\RequestUri as Uri;

// Have some controllers
class FooCtrl
{
    public function getAll(Request $request): array { /* ... */ }
    public function post(Request $request): bool { /* ... */ }
}
class BarCtrl
{
    public function getById(Request $request, $fooId, $barId): Bar { /* ... */ }
}

// Create router
$router = new Router();

// Add routes and corresponding route handlers
$router->add(new Route(new Method(Method::GET), new Uri('/foo')), new RouteHandler(FooCtrl::class, 'getAll'));
$router->add(new Route(new Method(Method::POST), new Uri('/foo')), new RouteHandler(FooCtrl::class, 'post'));
$router->add(new Route(new Method(Method::GET), new Uri('/foo/{id}/bar/{id}')), new RouteHandler(BarCtrl::class, 'getById'));

// Resolve a handler for an incoming request
$routeHandler = $router->resolve(new Route($request->getMethod(), $request->getUri()));

// (Optional) Resolve the handler class from the container and call the handling method 
$response = $container
    ->resolve($routeHandler->getClassName())
    ->{$routeHandler->getMethodName()}($request, ...$routeHandler->getRoute()->getCapturedSegments());

How it works

TBD, (*7)

Tests

$ vendor/bin/phpunit
$ composer phpcs
$ composer phpstan

Changelog

Click here, (*8)

Licence

MIT, (*9)

The Versions

31/10 2017

dev-master

9999999-dev https://github.com/pwm/sfw-router

A simple Router that maps incoming requests to predefined handlers

  Sources   Download

MIT

The Requires

 

The Development Requires

by Zsolt Szende

http-router sfw

03/07 2017

1.1.0

1.1.0.0

Simple Router for JSON APIs

  Sources   Download

MIT

The Requires

 

The Development Requires

by Zsolt Szende

27/04 2017

1.0.2

1.0.2.0

Simple Router for JSON APIs

  Sources   Download

MIT

The Requires

 

The Development Requires

by Zsolt Szende

27/04 2017

1.0.1

1.0.1.0

Simple Router for JSON APIs

  Sources   Download

MIT

The Requires

 

The Development Requires

27/04 2017

1.0.0

1.0.0.0

Simple Router for JSON APIs

  Sources   Download

MIT

The Requires

 

The Development Requires