zonuexe / simple-routing
简单Web应用路由器
0.6.0
2023-07-31 12:30 UTC
Requires
- php: >=5.6
- zonuexe/objectsystem: ~0.5|~0.6
Requires (Dev)
- phpunit/phpunit: ^8.5|^7.5|^4.8
- yoast/phpunit-polyfills: ^1.0
README
WebApp的简单路由
无 魔法。无 反射。无 复杂依赖。
安装
Composer
composer require zonuexe/simple-routing
参考资料
- API文档
- ja: 简单的路由实现 - Qiita
路由DSL
// Method Path ReturnValue Param => RegExp extension (format) $routing_map = [ ['GET', '/', 'index' ], ['GET|POST', '/search', 'search' ], ['GET', '/article/:id', 'article', ['id' => '/\A(\d+)\z/'], '?ext' => ['', 'txt']], ['GET', '/info', 'feed' , '?ext' => ['rss', 'rdf', 'xml']], '#404' => 'not_found' // special ]; $router = new \Teto\Routing\Router($routing_map); $action = $router->match($_SERVER['REQUEST_METHOD'], $_SERVER['REQUEST_URI']); // Shorthand (but, do not use reverse routing) $action = \Teto\Routing\Router::dispatch($routing_map, $_SERVER['REQUEST_METHOD'], $_SERVER['REQUEST_URI']);
反向路由
// Name Method Path ReturnValue Param => RegExp extension (file format) $routing_map = [ 'root' => ['GET', '/', 'index' ], 'search' => ['GET|POST', '/search', 'search' ], 'article' => ['GET', '/article/:id', 'article', ['id' => '/\A(\d+)\z/'], '?ext' => ['', 'txt']], 'info' => ['GET', '/info', 'feed' , '?ext' => ['rss', 'rdf', 'xml']], '#404' => 'not_found' // special ]; $router = new \Teto\Routing\Router($routing_map); $router->makePath('root'); //=> '/' $router->makePath('search'); //=> '/search' $router->makePath('article', ['id' => 123]); //=> '/article/123' $router->makePath('info', ['?ext' => 'rss']); //=> '/info.rss'
技巧
$re_num_id = '/\A(\d+)\z/'; $re_user_name = '/\A@([a-z]+)\z/'; $routing_map = [ 'root' => ['GET', '/', 'index' ], 'search' => ['GET', '/search', 'search' ], 'article' => ['GET', '/article/:id', 'article', ['id' => $re_num_id], '?ext' => ['', 'txt']], '#404' => 'not_found' // special ];
相关库
您可以从Packagist获取这些库。
版权
Teto Routing 采用 Apache许可证2.0版。请参阅 ./LICENSE
。
Teto Routing - PHP simple router for WebApp
Copyright (c) 2016 Baguette HQ / USAMI Kenta <tadsan@zonu.me>
Teto Kasane
我喜欢 Teto Kasane。 (ja: Teto Kasane官方网站)
r /
__ , --ヽ!-- .、_
! `/::::;::::ヽ l
!二二!::/}::::丿ハニ|
!ニニ.|:/ ノ/ }::::}コ
L二lイ 0´ 0 ,':ノコ
lヽノ/ヘ、 '' ▽_ノイ ソ
ソ´ /}`ス / ̄ ̄ ̄ ̄/
.(_:;つ/ 0401 / カタカタ
 ̄ ̄ ̄ ̄ ̄\/____/