Skip to content

Commit 2b0af38

Browse files
committed
Improve readme
1 parent 0defe65 commit 2b0af38

1 file changed

Lines changed: 193 additions & 1 deletion

File tree

README.md

Lines changed: 193 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,196 @@
11
Cortex
22
======
33

4-
**Cortex is routing system for WordPress** based on [FastRoute](https://github.com/nikic/FastRoute)
4+
**Cortex is routing system for WordPress** based on [FastRoute](https://github.com/nikic/FastRoute)
5+
6+
## Start using Cortex
7+
8+
First of all ensure Composer autoload is loaded.
9+
10+
After that "boot" Cortex:
11+
12+
```php
13+
Brain\Cortex::boot();
14+
```
15+
16+
This can be done as soon as you can, no need to wrap in a hook.
17+
18+
It will not work after [`'do_parse_request'`](https://developer.wordpress.org/reference/hooks/do_parse_request/)
19+
as been fired.
20+
21+
## Adding routes
22+
23+
To add routes, it is possible to use `'cortex.routes'` hook, that passes an instance of
24+
`RouteCollectionInterface`:
25+
26+
```php
27+
use Brain\Cortex\Route\RouteCollectionInterface as Routes;
28+
use Brain\Cortex\Route\QueryRoute;
29+
30+
add_action('cortex.routes', function(Routes $routes) {
31+
32+
$routes->addRoute(new QueryRoute(
33+
'^{type:[a-z]+}/latest$',
34+
function(array $matches) {
35+
return [
36+
'post_type' => $matches['type'],
37+
'posts_per_page' => 5,
38+
'orderby' => 'date',
39+
'order' => 'ASC'
40+
];
41+
}
42+
));
43+
});
44+
```
45+
46+
The route pattern (1st argument) syntax is inherited from FastRoute.
47+
48+
The callback passed as second argument receives the array of matches (`$routeInfo[2]` in FastRoute)
49+
and has to return an array of arguments for `WP_Query`.
50+
51+
52+
##`QueryRoute` arguments
53+
54+
`QueryRoute` constructor accepts as 3rd argument an array of options for
55+
route configuration.
56+
57+
One of them is **"template"** to force WordPress use a template when the route matches:
58+
59+
add_action('cortex.routes', function(Routes $routes) {
60+
61+
$routes->addRoute(new QueryRoute(
62+
'^post/latest$',
63+
function(array $matches) {
64+
return [
65+
'orderby' => 'date',
66+
'order' => 'DESC'
67+
];
68+
},
69+
['template' => 'latest.php']
70+
));
71+
});
72+
```
73+
74+
There are other arguments, among them:
75+
76+
- "before" and "after", that are callbacks run respectively before and after the
77+
callback that returns query arguments is called
78+
- "host" to make the route match only for specific host
79+
- "method" to make the route match only for specific HTTP method (e.g. `POST` or `GET`)
80+
- "scheme" to make the route match only for specific HTTP scheme (e.g. `https` or `http`)
81+
- "group" to use configuration from one or more "route groups"
82+
- "priority" to force the route evaluation in specific order (lower priority first)
83+
- "merge_query_string" to allow (default) or avoid url query string are merged as
84+
query argument to anything returned by route callback
85+
86+
## Route groups
87+
88+
A route group is a way to share common settings among routes.
89+
90+
Before assign groups to routes, we need to add groups.
91+
92+
That can be done using `'cortex.groups'` hook, that pass an instance of `GroupCollectionInterface`:
93+
94+
```php
95+
use Brain\Cortex\Route\RouteCollectionInterface as Routes;
96+
use Brain\Cortex\Group\GroupCollectionInterface as Groups;
97+
use Brain\Cortex\Route\QueryRoute;
98+
use Brain\Cortex\Group\Group;
99+
100+
add_action('cortex.groups', function(Groups $groups) {
101+
102+
$groups->addGroup(new Group([
103+
'id' => 'archive-group',
104+
'template' => 'archive.php',
105+
'before' => function() {
106+
// do something before route callback
107+
}
108+
]));
109+
});
110+
111+
add_action('cortex.routes', function(Routes $routes) {
112+
113+
$routes->addRoute(new QueryRoute(
114+
'^post/latest$',
115+
function(array $matches) {
116+
return [
117+
'orderby' => 'date',
118+
'order' => 'DESC'
119+
];
120+
},
121+
['group' => 'archive-group']
122+
));
123+
124+
$routes->addRoute(new QueryRoute(
125+
'^post/oldest',
126+
function(array $matches) {
127+
return [
128+
'orderby' => 'date',
129+
'order' => 'ASC'
130+
];
131+
},
132+
['group' => 'archive-group']
133+
));
134+
});
135+
```
136+
137+
A group is instantiated passing an array of values to its constructor.
138+
The value "id" is required. All other values are optional, and can be used to set
139+
any route property (array items in 3rd param of `QueryRoute` constructor).
140+
141+
To use properties from a group in a route, the group id has to be set in the `'group'`
142+
route property.
143+
144+
`'group'` property also accepts an array of group ids, to assign properties
145+
from multiple groups.
146+
147+
148+
## Redirect routes
149+
150+
`QueryRoute` is just one of the routes shipped with Cortex.
151+
There are others and it is possible to write custom routes implementing `Brain\Cortex\Route\RouteInterface`.
152+
153+
Another implementation included in Cortex is `RedirectRoute`. As the name suggests,
154+
it is used to redirect urls to other urls.
155+
156+
```php
157+
use Brain\Cortex\Route\RouteCollectionInterface as Routes;
158+
use Brain\Cortex\Route\RedirectRoute;
159+
160+
add_action('cortex.routes', function(Routes $routes) {
161+
162+
$routes->addRoute(new RedirectRoute(
163+
'^old/url/{postname}$',
164+
function(array $matches) {
165+
return 'new/url/' . $matches['postname'];
166+
}
167+
));
168+
});
169+
```
170+
171+
`RedirectRoute` accepts an array of options as well.
172+
173+
Using option is possible to configure HTTP status code to use (`'redirect_status'` option, default 302)
174+
and if allows or not redirect to external urls (`'redirect_external'` option, default false).
175+
176+
177+
----------
178+
179+
180+
## Installation
181+
182+
Via Composer, require `brain\cortex` in version `~1.0.0`.
183+
184+
## Minimum Requirements
185+
186+
- PHP 5.5+
187+
- Composer to install
188+
189+
## Dependencies
190+
191+
- Any version of PSR7 interfaces (no implementation required)
192+
- FastRoute
193+
194+
## License
195+
196+
MIT

0 commit comments

Comments
 (0)