PHPackages                             brain/cortex - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. brain/cortex

ActiveLibrary

brain/cortex
============

Cortex is a package that implements a routing system in WordPress.

0.1.0(10y ago)347197.7k↓41.8%22[1 issues](https://github.com/Brain-WP/Cortex/issues)4GPL-2.0+PHPPHP &gt;=5.4

Since Feb 12Pushed 2y ago12 watchersCompare

[ Source](https://github.com/Brain-WP/Cortex)[ Packagist](https://packagist.org/packages/brain/cortex)[ Docs](https://github.com/Brain-WP/Cortex)[ RSS](/packages/brain-cortex/feed)WikiDiscussions refactoring-fastroute Synced 1mo ago

READMEChangelog (10)Dependencies (8)Versions (14)Used By (4)

Cortex
======

[](#cortex)

[![Travis CI](https://camo.githubusercontent.com/563e9c7a104ceb7db923da28a3d1deaa21acf22ed24111349cd609a270d6703f/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f427261696e2d57502f436f727465782e7376673f6272616e63683d7265666163746f72696e672d66617374726f757465267374796c653d666c61742d737175617265)](https://travis-ci.org/Brain-WP/Cortex)[![codecov.io](https://camo.githubusercontent.com/9f8e28a3ea6eb718893a2580aea63b10310be378e428d8ce81a23f5429b29e66/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f6769746875622f427261696e2d57502f436f727465782e7376673f7374796c653d666c61742d737175617265266272616e63683d7265666163746f72696e672d66617374726f757465)](https://codecov.io/github/Brain-WP/Cortex?branch=refactoring-fastroute)[![MIT license](https://camo.githubusercontent.com/d455e95f506450f76767cbd942335c3f9203c7d121d85b3d3d3b8d2e0dcf65e1/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f627261696e2f636f727465782e7376673f7374796c653d666c61742d737175617265)](http://opensource.org/licenses/MIT)

---

**Cortex is routing system for WordPress** based on [FastRoute](https://github.com/nikic/FastRoute)

Start using Cortex
------------------

[](#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'`](https://developer.wordpress.org/reference/hooks/do_parse_request/)has been fired.

Adding routes
-------------

[](#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-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
------------

[](#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
---------------

[](#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
------------

[](#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
--------------------

[](#minimum-requirements)

- PHP 5.5+
- Composer to install

Dependencies
------------

[](#dependencies)

- Any version of PSR7 interfaces (no implementation required)
- FastRoute

License
-------

[](#license)

MIT

###  Health Score

41

—

FairBetter than 89% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity52

Moderate usage in the ecosystem

Community28

Small or concentrated contributor base

Maturity55

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 95.3% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~252 days

Recently: every ~655 days

Total

12

Last Release

974d ago

Major Versions

0.1.0 → 1.0.0-alpha.42016-02-23

PHP version history (2 changes)1.0.0-alpha.1PHP &gt;=5.4

1.0.0-alpha.4PHP &gt;=5.5

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/2208282?v=4)[Giuseppe Mazzapica](/maintainers/gmazzap)[@gmazzap](https://github.com/gmazzap)

---

Top Contributors

[![gmazzap](https://avatars.githubusercontent.com/u/2208282?v=4)](https://github.com/gmazzap "gmazzap (202 commits)")[![pongho](https://avatars.githubusercontent.com/u/1329964?v=4)](https://github.com/pongho "pongho (3 commits)")[![tfrommen](https://avatars.githubusercontent.com/u/6049306?v=4)](https://github.com/tfrommen "tfrommen (3 commits)")[![rtpHarry](https://avatars.githubusercontent.com/u/1038062?v=4)](https://github.com/rtpHarry "rtpHarry (1 commits)")[![polevaultweb](https://avatars.githubusercontent.com/u/1770201?v=4)](https://github.com/polevaultweb "polevaultweb (1 commits)")[![metaline](https://avatars.githubusercontent.com/u/1336002?v=4)](https://github.com/metaline "metaline (1 commits)")[![audvin](https://avatars.githubusercontent.com/u/1017880?v=4)](https://github.com/audvin "audvin (1 commits)")

---

Tags

fastrouterouterroutingwordpresswordpress

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/brain-cortex/health.svg)

```
[![Health](https://phpackages.com/badges/brain-cortex/health.svg)](https://phpackages.com/packages/brain-cortex)
```

###  Alternatives

[hwi/oauth-bundle

Support for authenticating users using both OAuth1.0a and OAuth2 in Symfony.

2.4k21.5M69](/packages/hwi-oauth-bundle)[wpstarter/framework

The WpStarter Framework - Laravel Framework for WordPress

1810.1k4](/packages/wpstarter-framework)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
