2017 © Pedro Peláez
 

library router

Kambo router

image

kambo/router

Kambo router

  • Thursday, July 6, 2017
  • by kambo
  • Repository
  • 1 Watchers
  • 0 Stars
  • 5 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 0 % Grown

The README.md

Kambo PHP router

Build Status Scrutinizer Code Quality Code Coverage Software License, (*1)

Just another PHP router with following highlights:, (*2)

  • Support of PSR-7 - HTTP message interfaces
  • Two dispatchers with closure and controller/module support
  • Can be used even without mod_rewrite

Install

Prefered way to install library is with composer:, (*3)

composer require kambo/router

Usage

URL rewrite

For rewrite support in Apache with enabled mod_rewrite create a .htaccess file in your root directory, with following settings:, (*4)

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(.*)$ index.php [NC,L]

For disabling support of mod_rewrite use method setUrlFormat:, (*5)

$matcher->setUrlFormat(RouteMode::GET_FORMAT);

Route definition

The routes are added by calling createRoute() on the RouteCollection instance:, (*6)

$routeCollection->createRoute($method, $routePattern, $handler);

The $method is HTTP method name represented by value from Kambo\Router\Enum\Method enum for which a certain route should match, eg.: Method::GET, (*7)

By default the $routePattern uses a syntax where {foo} specifies a placeholder with name foo and matching the regex [^/]+. To adjust the pattern the placeholder matches, you can specify a custom pattern by writing {bar:[0-9]+}. Some examples:, (*8)

// Matches /user/kambo/123, but not /user/kambo/abc
$routeCollection->addRoute(Method::GET, '/user/{name}/{id:\d+}', $handler);

A shortcut methods can be also used for all Method:, (*9)

// Shortcut for createRoute(Method::GET, '/user/{name}/{id:\d+}', $handler);

$routeCollection->get('/user/{name}/{id:\d+}', $handler) 
$routeCollection->post('post/url', $handler)
$routeCollection->delete('delete/url', $handler)
$routeCollection->put('put/url', $handler)
$routeCollection->any('any/url', $handler)

A closure as $handler can be used:, (*10)

$routeCollection->get('/article/{id:\d+}', function($id) {
    echo $id;
});

PSR-7 - HTTP message interfaces

Kambo router is using a instance of PSR 7 compatible request object for abstraction over server variables. Any third party library that implements PSR-7 can be used, such as Kambo/HttpMessage, (*11)

Router dispatcher

Router comes with following dispatcher:, (*12)

  • Closure dispatcher with automatic path = closure variable bind function.
  • Opinionated class dispatcher which force you organize your code into module/controller class structure.

Using closure dispatcher

<?php

// Kambo\Router
use Kambo\Router\Route\Collection;
use Kambo\Router\Route\Builder\Base;
use Kambo\Router\Dispatcher\ClosureAutoBind;
use Kambo\Router\Router;
use Kambo\Router\Matcher\Regex;

// Kambo\Http\Message
use Kambo\Http\Message\Environment\Environment;
use Kambo\Http\Message\Factories\Environment\ServerRequestFactory;

$routeCollection = new Collection(new Base());

// Matches http://{domain}/user/{string}/transaction/{integer number} eg.: http://router-example.vg/user/kambo/transaction/1
$routeCollection->get('/user/{name}/transaction/{id:\d+}', function(int $id, string $name) {
    echo $id.' '.$name;
});

// Matches http://{domain}/article/{integer number} eg.: http://router-example.vg/article/42
$routeCollection->get('/article/{id:\d+}', function(int $id) {
    echo 'article id: '.$id;
});

// Create instance of the closure dispatcher with function properties auto bind functionality
$dispatcherClosureAutoBind = new ClosureAutoBind();

// Create instance of the route matcher based on regular expressions
$matcherRegex = new Regex($routeCollection);

// Create instance of the Router
$router = new Router($dispatcherClosureAutoBind, $matcherRegex);

// Create Environment object based on server variables.
$environment = new Environment($_SERVER, fopen('php://input', 'w+'), $_POST, $_COOKIE, $_FILES);

// Create instance of ServerRequest object in this example we are using Kambo/HttpMessage (https://github.com/kambo-1st/HttpMessage)
// but any other implementation of PSR 7 server request can be used.
$request = (new ServerRequestFactory())->create($environment);

// Start URL matching a PSR 7 compatible object must be provided
$router->dispatch($request);

This example will define two routes:, (*13)

http://{domain}/user/{string}/transaction/{integer number} http://{domain}/article/{integer number}, (*14)

License

The MIT License (MIT), https://opensource.org/licenses/MIT, (*15)

The Versions

06/07 2017

dev-master

9999999-dev

Kambo router

  Sources   Download

MIT Apache-2.0

The Requires

 

The Development Requires

by Bohuslav Simek

routing router

06/07 2017

v0.9

0.9.0.0

Kambo router

  Sources   Download

MIT

The Requires

 

The Development Requires

by Bohuslav Simek

routing router

06/07 2017

dev-develop

dev-develop

Kambo router

  Sources   Download

MIT

The Requires

 

The Development Requires

by Bohuslav Simek

routing router

30/11 2016

v0.9.0

0.9.0.0

Kambo router

  Sources   Download

Apache-2.0

The Requires

 

by Bohuslav Simek

routing router