定制 leeoniya/route66 二次开发

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

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

leeoniya/route66

Composer 安装命令:

composer require leeoniya/route66

包简介

PHP micro-router

README 文档

README

PHP micro-router (MIT Licensed)

  • Concise, simple syntax wrapping a full feature set
  • Easy install; Composer or single file include (~200 SLOC)
  • Low routing overhead (< 1ms)*

Route66 was written to add some features to the minimalist Macaw router without bloating it.

Nginx config example

server {
	index index.html index.htm index.php;

	location / {
		try_files $uri $uri/ /index.php?/$uri;
	}

	location ~ \.php$ {
		fastcgi_pass 127.0.0.1:9000;
		fastcgi_index index.php;
		fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
		include fastcgi_params;
	}
}

Install (require or Composer)

require 'Route66.php';

https://packagist.org/packages/leeoniya/route66

{
	"require": {
		"leeoniya/route66": "dev-master"
	}
}

Basic example

// front controller (index.php)
require 'Route66.php';

use Route66 as R;

R::get('/', function() {
	echo 'Hello world!';
});

R::dispatch();

HTTP methods

// any HTTP method can be bound via static invocation using its name
R::post('/topics', function() {});

// multiple methods
R::match('post|put', '/comment', function() {});

// equivalent to R::match('get|post|put|patch|delete|head|options',..)
R::any('/', function() {};)

Named params & validation

// basic param (all characters except '/')
R::get('/posts/@id',       function($id) {});

// param validated by regex alias
R::get('/posts/@id:alpha', function($id) {});

// aliases and regexs can be broken out for readability
R::get('/posts/@id',       function($id) {}, ['id' => ':alpha']);
R::get('/posts/@id',       function($id) {}, ['id' => '\w{12}']);

// define a custom alias
R::alias(':date', '[0-9]{4}-[0-9]{2}-[0-9]{2}');

Optional segments & params

// optional params with defaults (set in handler)
R::get('/posts(/@year(/@month(/@day)))', function($year = 2015, $month = 6, $day = 15) {});

// regex alias without a param (non-capturing)
R::get('/posts(/:slug)', function() {});

// optional trailing slash (though rtrim-ing it from REQUEST_URI before dispatch is faster)
R::get('/blog/?', function() {});

Un-named params

// un-named params via regex capture groups
R::get('/posts/(\w{12})', function($id) {});

Route prefixing (base path)

R::base('/blog');
R::get('/posts', function() {});	// maps to /blog/posts

R::base('/admin');
R::get('/login', function() {});	// maps to /admin/login

Before pre-filter (fall-through)

// before all /admin* routes
R::get('/admin(/:all)', function() {
	// verify valid session, etc...
	return R::NOHALT;		// fall through to additional routes
});

R::get('/admin/dashboard', function() {
	// show dashboard...
});

Custom catch-all (no route found)

R::nomatch(function($meth, $uri) {
	header($_SERVER["SERVER_PROTOCOL"] . " 404 Not Found");
	exit('404 Not Found.');
});

Custom dispatch (e.g. internal redirects)

R::dispatch('get', '/blog');

Route caching

// example of storing compiled routes in session
session_start();

if ($routes = @$_SESSION['routes'])
	R::import($routes);

// define all routes here

R::dispatch();

if (!isset($_SESSION['routes']))
	$_SESSION['routes'] = R::export();

Named routes & reverse routing

// un-named
R::get('/users/@id', function($id) {}, ['id'=>':alpha']);

// named (the trailing 2 args can be passed in any order. all below are equivalent.)
R::get('/users/@id', function($id) {}, 'named1');
R::get('/users/@id', function($id) {}, ['id'=>':alpha'], 'named1');
R::get('/users/@id', function($id) {}, 'named1', ['id'=>':alpha']);

// dispatch a named route
R::dispatch('get', 'named1', ['id' => 'abc123']);

Util methods

R::is_ajax();
R::is_https();
R::redirect($location, $code = 301);

New features (relative to Macaw)

  • Named params (e.g. @user:alpha)
  • Optional route segments and params, param defaults
  • Route-group prefixing
  • Param regex & aliases can be broken out for readability
  • Short defs for multi-method routes
  • Named routes and reverse routing, custom dispatch
  • Per-route fall-through control (Macaw has only global)
  • Route compilation cache, import/export
  • Utility methods for is_ajax, is_https and redirect
  • Modified and added regex aliases

Other PHP routers

* As with any router, speed will depend on: route quantity & complexity, numbers of params, whether caching is used, server hardware, PHP version, opcode caching.

leeoniya/route66 适用场景与选型建议

leeoniya/route66 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 871 次下载、GitHub Stars 达 15, 最近一次更新时间为 2015 年 01 月 03 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 871
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 15
  • 点击次数: 22
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 15
  • Watchers: 5
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-01-03