brain/cortex
Composer 安装命令:
composer require brain/cortex
包简介
Cortex is a package that implements a routing system in WordPress.
README 文档
README
Cortex is routing system for WordPress based on FastRoute
Start using Cortex
First of all ensure Composer autoload is loaded.
After that "boot" Cortex:
Brain\Cortex::boot();
This can be done as soon as you can, no need to wrap in a hook.
It will not work after 'do_parse_request'
has been fired.
Adding routes
To add routes, it is possible to use 'cortex.routes' hook, that passes an instance of
RouteCollectionInterface:
use Brain\Cortex\Route\RouteCollectionInterface; use Brain\Cortex\Route\QueryRoute; add_action('cortex.routes', function(RouteCollectionInterface $routes) { $routes->addRoute(new QueryRoute( '{type:[a-z]+}/latest', function(array $matches) { return [ 'post_type' => $matches['type'], 'posts_per_page' => 5, 'orderby' => 'date', 'order' => 'ASC' ]; } )); });
The route pattern (1st argument) syntax is inherited from FastRoute.
The callback passed as second argument receives the array of matches ($routeInfo[2] in FastRoute)
and has to return an array of arguments for WP_Query.
QueryRoute arguments
QueryRoute constructor accepts as 3rd argument an array of options for
route configuration.
One of them is "template" to force WordPress use a template when the route matches:
add_action('cortex.routes', function(RouteCollectionInterface $routes) { $routes->addRoute(new QueryRoute( 'post/latest', function(array $matches) { return [ 'orderby' => 'date', 'order' => 'DESC' ]; }, ['template' => 'latest.php'] )); });
As shown above,template argument can be a relative path to theme (or child theme) folder.
To use a template that resides outside theme folder, template argument need to be full absolute path
to the template file to use.
There are other arguments, among them:
- "before" and "after", that are callbacks run respectively before and after the callback that returns query arguments is called
- "host" to make the route match only for specific host
- "method" to make the route match only for specific HTTP method (e.g.
POSTorGET) - "scheme" to make the route match only for specific HTTP scheme (e.g.
httpsorhttp) - "group" to use configuration from one or more "route groups"
- "priority" to force the route evaluation in specific order (lower priority first)
- "merge_query_string" to allow (default) or avoid url query string are merged as query argument to anything returned by route callback
Route groups
A route group is a way to share common settings among routes.
Before assign groups to routes, we need to add groups.
That can be done using 'cortex.groups' hook, that pass an instance of GroupCollectionInterface:
use Brain\Cortex\Route\RouteCollectionInterface; use Brain\Cortex\Group\GroupCollectionInterface; use Brain\Cortex\Route\QueryRoute; use Brain\Cortex\Group\Group; add_action('cortex.groups', function(GroupCollectionInterface $groups) { $groups->addGroup(new Group([ 'id' => 'archive-group', 'template' => 'archive.php', 'before' => function() { // do something before route callback } ])); }); add_action('cortex.routes', function(RouteCollectionInterface $routes) { $routes->addRoute(new QueryRoute( '^post/latest$', function(array $matches) { return [ 'orderby' => 'date', 'order' => 'DESC' ]; }, ['group' => 'archive-group'] )); $routes->addRoute(new QueryRoute( 'post/oldest', function(array $matches) { return [ 'orderby' => 'date', 'order' => 'ASC' ]; }, ['group' => 'archive-group'] )); });
A group is instantiated passing an array of values to its constructor.
The value "id" is required. All other values are optional, and can be used to set
any route property (array items in 3rd param of QueryRoute constructor).
To use properties from a group in a route, the group id has to be set in the 'group'
route property.
'group' property also accepts an array of group ids, to assign properties
from multiple groups.
Redirect routes
QueryRoute is just one of the routes shipped with Cortex.
There are others and it is possible to write custom routes implementing Brain\Cortex\Route\RouteInterface.
Another implementation included in Cortex is RedirectRoute. As the name suggests,
it is used to redirect urls to other urls.
use Brain\Cortex\Route\RouteCollectionInterface; use Brain\Cortex\Route\RedirectRoute; add_action('cortex.routes', function(RouteCollectionInterface $routes) { $routes->addRoute(new RedirectRoute( 'old/url/{postname}', function(array $matches) { return 'new/url/' . $matches['postname']; } )); });
RedirectRoute accepts an array of options as well.
Using option is possible to configure HTTP status code to use ('redirect_status' option, default 302)
and if allows or not redirect to external urls ('redirect_external' option, default false).
Installation
Via Composer, require brain/cortex in version ~1.0.0.
composer require brain/cortex:~1.0.0
You may need to lessen your project's minimum stability requirements.
composer config minimum-stability dev
Minimum Requirements
- PHP 5.5+
- Composer to install
Dependencies
- Any version of PSR7 interfaces (no implementation required)
- FastRoute
License
MIT
brain/cortex 适用场景与选型建议
brain/cortex 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 202.41k 次下载、GitHub Stars 达 347, 最近一次更新时间为 2014 年 05 月 31 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「routing」 「wordpress」 「router」 「fast route」 「rewrite rules」 「pretty permalink」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 brain/cortex 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 brain/cortex 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 brain/cortex 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Write down your routing mapping at one place
Flight routing is a simple, fast PHP router that is easy to get integrated with other routers.
Router
PMVC App Action Router
Database alias router extension for Nette Framework
Client for Google Directions API to add interpolated points to a route consisting of given coordinates.
统计信息
- 总下载量: 202.41k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 350
- 点击次数: 25
- 依赖项目数: 5
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2014-05-31