All Projects → Brain-WP → Cortex

Brain-WP / Cortex

Licence: mit
Routing system for WordPress

Projects that are alternatives of or similar to Cortex

Simple Php Router
Simple, fast and yet powerful PHP router that is easy to get integrated and in any project. Heavily inspired by the way Laravel handles routing, with both simplicity and expand-ability in mind.
Stars: ✭ 279 (-7%)
Mutual labels:  router, routing
Fluro
Fluro is a Flutter routing library that adds flexible routing options like wildcards, named parameters and clear route definitions.
Stars: ✭ 3,372 (+1024%)
Mutual labels:  router, routing
STCRouter
基于标准URL的iOS路由系统,可实现业务模块组件化,控制器之间零耦合,可实现黑白名单控制,可进行native降级到hybrid。
Stars: ✭ 19 (-93.67%)
Mutual labels:  router, routing
neteng-roadmap
Network Engineering at Scale Roadmap/Landscape
Stars: ✭ 53 (-82.33%)
Mutual labels:  router, routing
react-mobx-router5
React components for routing solution using router5 and mobx
Stars: ✭ 58 (-80.67%)
Mutual labels:  router, routing
RouteNow
RouteNow is a small fast library ⚡ that will help you in developing a SinglePage Application without any dependencies like jQuery, AngularJs, vue.js or any of those bulky frameworks.
Stars: ✭ 17 (-94.33%)
Mutual labels:  router, routing
CRRouter
A simple and powerful router
Stars: ✭ 54 (-82%)
Mutual labels:  router, routing
Clevergo
👅 CleverGo is a lightweight, feature rich and high performance HTTP router for Go.
Stars: ✭ 246 (-18%)
Mutual labels:  router, routing
r5r
ipeagit.github.io/r5r/
Stars: ✭ 90 (-70%)
Mutual labels:  router, routing
go router
The purpose of the go_router for Flutter is to use declarative routes to reduce complexity, regardless of the platform you're targeting (mobile, web, desktop), handling deep linking from Android, iOS and the web while still allowing an easy-to-use developer experience.
Stars: ✭ 380 (+26.67%)
Mutual labels:  router, routing
routex.js
🔼 Alternative library to manage dynamic routes in Next.js
Stars: ✭ 38 (-87.33%)
Mutual labels:  router, routing
gatsby-plugin-dynamic-routes
Creating dynamic routes based on your environment and/or renaming existing routes
Stars: ✭ 14 (-95.33%)
Mutual labels:  router, routing
journey
A conductor routing helper library
Stars: ✭ 35 (-88.33%)
Mutual labels:  router, routing
Abstract State Router
Like ui-router, but without all the Angular. The best way to structure a single-page webapp.
Stars: ✭ 288 (-4%)
Mutual labels:  router, routing
es6-router
🌐 Simple client side router built in ES6
Stars: ✭ 16 (-94.67%)
Mutual labels:  router, routing
router
Bidirectional Ring router. REST oriented. Rails inspired.
Stars: ✭ 78 (-74%)
Mutual labels:  router, routing
Router
Router implementation for fasthttp
Stars: ✭ 234 (-22%)
Mutual labels:  router, routing
Swiftuirouter
Routing in SwiftUI
Stars: ✭ 242 (-19.33%)
Mutual labels:  router, routing
OpenBSDFirewall
Simple OpenBSD Home Firewall Config for ALIX Board
Stars: ✭ 41 (-86.33%)
Mutual labels:  router, routing
yew-router
Router extension to yew
Stars: ✭ 27 (-91%)
Mutual labels:  router, routing

Cortex

Travis CI codecov.io MIT license


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' as 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. POST or GET)
  • "scheme" to make the route match only for specific HTTP scheme (e.g. https or http)
  • "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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].