PHP code example of brain / cortex
1. Go to this page and download the library: Download brain/cortex library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
brain / cortex example snippets
Brain\Cortex::boot();
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'
];
}
));
});
add_action('cortex.routes', function(RouteCollectionInterface $routes) {
$routes->addRoute(new QueryRoute(
'post/latest',
function(array $matches) {
return [
'orderby' => 'date',
'order' => 'DESC'
];
},
['template' => 'latest.php']
));
});
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']
));
});
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'];
}
));
});