alexpts/php-routing

Simple router compatible with the PSR-7

1.0.0 2017-09-05 18:28 UTC

This package is auto-updated.

Last update: 2024-05-15 00:59:45 UTC


README

SensioLabsInsight

Build Status Code Coverage Code Climate Scrutinizer Code Quality

Простой роутер с поддержкой PSR-7 и middelware на уровне роута.

Возможности

  • Простой захват параметров из url
  • Использование RegExp для описания роута
  • Гибкие группировки для захвата параметров
  • Приоритеты роутов
  • Middlewares на уровне роутов
  • Высокая скорость работы
  • Указание дефолтного роута
  • Адаптирован для работы с REST

Простой роутинг

use PTS\Routing\Route;
use PTS\Routing\CollectionRoute;
use PTS\Routing\Matcher;
use PTS\Routing\RouteService;
use Psr\Http\Message\RequestInterface;

$route = new Route('/', function() {
    return ['response' => 'data'];
});
$collection = new CollectionRoute();
$collection->add('main', $route);
$matcher = new Matcher(new RouteService());

$activeRoute = $matcher->match($collection, '/')->current();
$response = $activeRoute($request); // PSR-7 request

Захват параметров из url

Захваченные параметры могут быть переданы в качестве аргументов в обработчик. Параметры начинающиеся с символа _ игнорируются. Они нужны для технических нужд.

use PTS\Routing\Route;
use PTS\Routing\CollectionRoute;
use PTS\Routing\Matcher;
use PTS\Routing\RouteService;
use Psr\Http\Message\RequestInterface;

$route = new Route('/users/{userId}/', function($userId) {
    return $userId;
});
$route->pushMiddleware(new CallWithMatchParams);

$collection = new CollectionRoute();
$collection->add('user', $route);
$matcher = new Matcher(new RouteService());

$activeRoute = $matcher->match($collection, '/users/4/')->current();