承接 windwalker/router 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

windwalker/router

Composer 安装命令:

composer require windwalker/router

包简介

Windwalker Router package

README 文档

README

Installation via Composer

Add this to the require block in your composer.json.

{
    "require": {
        "windwalker/router": "~3.0"
    }
}

Getting Started

use Windwalker\Router\Router;

$router = new Router;

Add Routes

use Windwalker\Route\Route;

// Route with name
$router->addRoute(new Route('sakura', 'flower/(id)/sakura', array('_controller' => 'SakuraController')));

// Route without name
$router->addRoute(new Route(null, 'flower/(id)/sakura', array('_controller' => 'SakuraController')));

Match Route

$route = $router->match('flower/12/sakura');

$variables = $route->getVariables(); // Array([_controller] => SakuraController)

// Use variables
$class = $variables['_controller'];

$controller = new $class;

Add More Options to Route

Route interface: '($name, $pattern[, $variables = array()][, $allowMethods = array()][, $options = array()])'

$route = new Route(
    'name',
    'pattern/of/route/(id).(format)',

    // Default Variables
    array(
        'id'    => 1,
        'alias' => 'foo-bar-baz',
        'format' => 'html'
    ),

    // Allow methods
    array('GET', 'POST'),

    // Options
    array(
        'host'    => 'windwalker.io',
        'scheme'  => 'http', // Only http & https
        'port'    => 80,
        'sslPort' => 443,
        'requirements' => array(
            'id' => '\d+'
        ),
        'extra' => array(
            '_ctrl' => 'Controller\Class\Name',
        )
    )
);

$router->addRoute($route);

// match routes
$route = $router->match(
    'pattern/of/route/25.html',
    array(
        'host'   => $uri->getHost(),
        'scheme' => $uri->getScheme(),
        'port'   => $uri->getPort()
    )
);

$variables = $route->getVariables();

// Merge these matched variables back to http request
$_REQUEST = array_merge($_REQUEST, $variables);

// Extra is the optional variables but we won't want to merge into request
$extra = $router->getExtra();

print_r($variables);
print_r($extra);

The printed result:

Array
(
    [id] => 25
    [alias] => foo-bar-baz
    [format] => html
)

Array(
    [_ctrl] => Controller\Class\Name
)

Build Route

build() is a method to generate route uri for view template.

$router->addRoute(new Route('sakura', 'flower/(id)/sakura', array('_controller' => 'SakuraController')));

$uri = $router->build('sakura', array('id' => 30)); // flower/30/sakura

echo '<a href="' . $uri . '">Link</a>';

Quick Mapping

addMap() is a simple method to quick add route without complex options.

$router->addMap('flower/(id)/sakura', array('_controller' => 'SakuraController', 'id' => 1));

$variables = $router->match('flower/30/sakura');

Rules

Simple Params

new Route(null, 'flower/(id)-(alias)');

Optional Params

Single Optional Params

new Route(null, 'flower(/id)');

Matched route could be:

flower
flower/25

Multiple Optional Params

new Route(null, 'flower(/year,month,day)');

Matched route could be:

flower
flower/2014
flower/2014/10
flower/2014/10/12

The matched variables will be

Array
(
    [year] => 2014
    [month] => 10
    [day] => 12
)

Wildcards

// Match 'king/john/troilus/and/cressida'
new Route(null, 'flower/(*tags)');

Matched:

Array
(
    [tags] => Array
    (
        [0] => john
        [1] => troilus
        [2] => and
        [3] => cressida
    )
)

Matchers

Windwalker Router provides some matchers to use different way to match routes.

Sequential Matcher

Sequential Matcher use the Sequential Search Method to find route. It is the slowest matcher but much more customizable. It is the default matcher of Windwalker Router.

use Windwalker\Router\Matcher\SequentialMatcher;

$router = new Router(array(), new SequentialMatcher);

Binary Matcher

Binary Matcher use the Binary Search Algorithm to find route. This matcher is faster than SequentialMatcher but it will break the ordering of your routes. Binary search will re-sort all routes by pattern characters.

use Windwalker\Router\Matcher\BinaryMatcher;

$router = new Router(array(), new BinaryMatcher);

Trie Matcher

Trie Matcher use the Trie tree to search route. This matcher is the fastest method of Windwalker Router, but the limit is that it need to use an simpler route pattern which is not as flexible as the other two matchers.

use Windwalker\Router\Matcher\TrieMatcher;

$router = new Router(array(), new TrieMatcher);

Rules of TrieMatcher

Simple Params

only match when the uri segments all exists. If you want to use optional segments, you must add two or more patterns.

flower
flower/:id
flower/:id/:alias

Wildcards

This pattern will convert segments after flower/ this to an array which named tags:

flower/*tags

Single Action Router

Single action router is a simple router that extends Windwalker Router. It just return a string if matched.

This is a single action controller example:

$router->addMap('flower/(id)/(alias)', 'FlowerController');

$controller = $router->match('flower/25/sakura');

$_REQUEST = array_merge($_REQUEST, $router->getVariables());

echo (new $controller)->execute();

Or a controller with action name:

$router->addMap('flower/(id)/(alias)', 'FlowerController::indexAction');

$matched = $router->match('flower/25/sakura');

$_REQUEST = array_merge($_REQUEST, $router->getVariables());

list($controller, $action) = explode('::', $matched);

echo (new $controller)->$action();

RestRouter

RestRouter is a simple router extends to SingleActionRouter, it can add some suffix of different methods.

$router->addMap('flower/(id)/(alias)', 'Flower\\Controller\\');

$controller = $router->match('flower/25/sakura', 'POST'); // Get Flower\\Controller\\Create

(new $controller)->execute();

Default Suffix mapping is:

'GET'     => 'Get',
'POST'    => 'Create',
'PUT'     => 'Update',
'PATCH'   => 'Update',
'DELETE'  => 'Delete',
'HEAD'    => 'Head',
'OPTIONS' => 'Options'

You can override it:

$router->setHttpMethodSuffix('POST', 'SaveController');

Exception

If Router not matched anything, it throws Windwalker\Router\Exception\RouteNotFoundException.

try
{
    $route = $router->match('flower/25');
}
catch (RouteNotFoundException $e)
{
    Application::close('Page not found', 404);
    
    exit();
}
catch (\Exception $e)
{
    // Do other actions...
}

windwalker/router 适用场景与选型建议

windwalker/router 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.69k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2014 年 12 月 16 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「framework」 「router」 「windwalker」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 windwalker/router 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 windwalker/router 我们能提供哪些服务?
定制开发 / 二次开发

基于 windwalker/router 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

  • Stars: 1
  • Watchers: 3
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: LGPL-2.0-or-later
  • 更新时间: 2014-12-16