Comments (2)
Well, it is not a Cortex feature and I think it's out of the scope of a router to make middlewares available.
With a little bit of code you can use Cortex with external libraries that provide middlewares.
To give you some directions, I think you can create you custom route class that accepts middlewares.
For example:
namespace Chris;
class MiddlewareQueryRoute extends \Brain\Cortex\Route\QueryRoute {
private $middlewares = [];
public function __construct($path, $queryBuilder, array $options = []) {
// store a "before" callback if it was already there
$before = empty($options['before']) ? null : $options['before'];
// then set before option to a function that 1st runs middlewares and
// after that runs the "before" callback if there was one
$options['before'] = function($vars, $wp, $template, $matches) use($before) {
$this->runMiddlewares($vars, $wp, $template, $matches);
is_callable($before) and $before($vars, $wp, $template, $matches);
};
parent::__construct($path, $queryBuilder, $options);
}
public function addMiddleware(callable $middleware) {
$this->middlewares[] = $middlewares;
return $this;
}
private function runMiddlewares($vars, $wp, $template, $matches) {
array_walk(
$this->middlewares,
function(callable $middleware) use($vars, $wp, $template, $matches) {
$middleware($vars, $wp, $template, $matches);
}
);
}
}
Having this class in place, you can create an instance of your route instead of Cortex QueryRoute
and use it to add middlewares, something like:
use Brain\Cortex\Route\RouteCollectionInterface as Routes;
use Chris\MiddlewareQueryRoute;
add_action('cortex.routes', function(Routes $routes) {
$path = '{type:[a-z]+}/latest';
$querybuilder = function(array $matches) {
return [ 'post_type' => $matches['type'] ];
};
$before = function($vars, $wp, $template, $matches) {
/* do something */
};
$route = new MiddlewareQueryRoute($path, $querybuilder, ['before' => $before])
->addMiddleware(function($vars, $wp, $template, $matches) {
/* do something */
})->addMiddleware(function($vars, $wp, $template, $matches) {
/* do something */
});
$routes->addRoute($route);
});
The "before" callback, if given, will be called after all the middleware callables have been ran.
Note that move from a code like this to a more sophisticated middleware implementation that makes use of, for example, "Relay" or any other middleware library out there, will not be that hard.
from cortex.
Brilliant, thank you!
from cortex.
Related Issues (20)
- Overriding `template_include` hook HOT 6
- Sage 9 Integration HOT 6
- Simple example not fired. HOT 1
- About template HOT 2
- [Question] Is there any way to give Cortext routes a higher priority over WP routes? HOT 5
- Preserving variables? HOT 1
- [Question] could you describe how to do a paginated route? HOT 1
- [feature request] Closures as a value for the `template` setting HOT 4
- Render specific post/page/tax archive HOT 1
- How to add body classes depending on a Cortex route - the right way. HOT 2
- Current status for this project? HOT 4
- AJAX requests handling - how to set up CORS? HOT 3
- Routes "players" and "players/" are treated as same url HOT 2
- Handle 404 error HOT 2
- Abandoned packages and Parse error | PHP 8.0.11 HOT 1
- post__in not working HOT 1
- Wordpress 6 Changing on do_parse_request HOT 1
- Issue with wordpress v6 HOT 4
- get_post() does not work
- PHP dependency conflict: "psr/http-message" now requires PHP 7.2 HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cortex.