定制 baryshev/tree-route 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

baryshev/tree-route

最新稳定版本:v2.0.0

Composer 安装命令:

composer require baryshev/tree-route

包简介

Perfomance focused request router

README 文档

README

TreeRoute is a performance focused request router with regular expressions support.

Installation

Install the latest version with composer require baryshev/tree-route

Usage

Basic usage:

<?php

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

$router = new \TreeRoute\Router();

// Defining route for several HTTP methods
$router->addRoute(['GET', 'POST'], '/', 'handler0');

// Defining route for one HTTP method
$router->addRoute('GET', '/news', 'handler1');

// Defining route with regular expression param
$router->get('/news/{id:[0-9]+}', 'handler2');

// Defining another route with regular expression param
$router->get('/news/{slug:[a-zA-Z\-]+}', 'handler3');

// Defining static route that conflicts with previous route, but static routes have high priority
$router->get('/news/all', 'handler4');

// Defining another route
$router->post('/news', 'handler5');

$method = 'GET';

// Optionally pass HEAD requests to GET handlers
// if ($method == 'HEAD') {
//    $method = 'GET';
// }

$url = '/news/1';

$result = $router->dispatch($method, $url);

if (!isset($result['error'])) {
    $handler = $result['handler'];
    $params = $result['params'];
    // Do something with handler and params
} else {
    switch ($result['error']['code']) {
        case 404 :
            // Not found handler here
            break;
        case 405 :
            // Method not allowed handler here
            $allowedMethods = $result['allowed'];
            if ($method == 'OPTIONS') {
                // OPTIONS method handler here
            }
            break;
    }
}

Save and restore routes (useful for routes caching):

<?php

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

$router = new \TreeRoute\Router();
$router->addRoute(['GET', 'POST'], '/', 'handler0');
$router->addRoute('GET', '/news', 'handler1');

$routes = $router->getRoutes();

$anotherRouter = new \TreeRoute\Router();
$anotherRouter->setRoutes($routes);

$method = 'GET';
$url = '/news';

$result = $anotherRouter->dispatch($method, $url);

Testing

composer install
./vendor/bin/codecept run

Benchmark

https://github.com/baryshev/FastRoute-vs-TreeRoute

统计信息

  • 总下载量: 791
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 68
  • 点击次数: 0
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 68
  • Watchers: 6
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2015-05-28