rareloop/wp-router
Composer 安装命令:
composer require rareloop/wp-router
包简介
Router
README 文档
README
This package is no longer supported. Use at your own risk. We recommend using the underlying router: https://github.com/Rareloop/router
Rare WordPress Router
A WordPress wrapper around the Rareloop PHP Router. Easily handle custom endpoints on your WordPress site with this plugin.
Installation
Although not a requirement, using Composer and a setup like Bedrock is the recommended installation method.
composer require rareloop/wp-router
Usage
Creating Routes
Map
Creating a route is done using the map function:
use Rareloop\WordPress\Router\Router; // Creates a route that matches the uri `/posts/list` both GET // and POST requests. Router::map(['GET', 'POST'], 'posts/list', function () { return 'Hello World'; });
map() takes 3 parameters:
methods(array): list of matching request methods, valid values:GETPOSTPUTPATCHDELETEOPTIONS
uri(string): The URI to match againstaction(function|string): Either a closure or a Controller string
Route Parameters
Parameters can be defined on routes using the {keyName} syntax. When a route matches that contains parameters, an instance of the RouteParams object is passed to the action.
Router::map(['GET'], 'posts/{id}', function(RouteParams $params) { return $params->id; });
Named Routes
Routes can be named so that their URL can be generated programatically:
Router::map(['GET'], 'posts/all', function () {})->name('posts.index'); $url = Router::url('posts.index');
If the route requires parameters you can be pass an associative array as a second parameter:
Router::map(['GET'], 'posts/{id}', function () {})->name('posts.show'); $url = Router::url('posts.show', ['id' => 123]);
HTTP Verb Shortcuts
Typically you only need to allow one HTTP verb for a route, for these cases the following shortcuts can be used:
Router::get('test/route', function () {}); Router::post('test/route', function () {}); Router::put('test/route', function () {}); Router::patch('test/route', function () {}); Router::delete('test/route', function () {}); Router::options('test/route', function () {});
Setting the basepath
The router assumes you're working from the route of a domain. If this is not the case you can set the base path:
Router::setBasePath('base/path'); Router::map(['GET'], 'route/uri', function () {}); // `/base/path/route/uri`
Controllers
If you'd rather use a class to group related route actions together you can pass a Controller String to map() instead of a closure. The string takes the format {name of class}@{name of method}. It is important that you use the complete namespace with the class name.
Example:
// TestController.php namespace \MyNamespace; class TestController { public function testMethod() { return 'Hello World'; } } // routes.php Router::map(['GET'], 'route/uri', '\MyNamespace\TestController@testMethod');
Creating Groups
It is common to group similar routes behind a common prefix. This can be achieved using Route Groups:
Router::group('prefix', function ($group) { $group->map(['GET'], 'route1', function () {}); // `/prefix/route1` $group->map(['GET'], 'route2', function () {}); // `/prefix/route2§` });
rareloop/wp-router 适用场景与选型建议
rareloop/wp-router 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.76k 次下载、GitHub Stars 达 16, 最近一次更新时间为 2017 年 07 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「router」 「rareloop」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 rareloop/wp-router 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 rareloop/wp-router 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 rareloop/wp-router 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A powerful PHP Router for PSR7 messages inspired by the Laravel API
PMVC App Action Router
Database alias router extension for Nette Framework
PHP Router
A PSR7 / PSR-15 compatible HTTP router.
Simple PHP Router, with prefix groups and filtering
统计信息
- 总下载量: 1.76k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 17
- 点击次数: 2
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-07-15