diff --git a/.gitignore b/.gitignore index a8c20201..c563c2be 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,9 @@ composer.lock vendor/ +# Mac OS +.DS_Store + # Auto-generated documentation directory (phpDocumentor) docs/ diff --git a/src/Klein/Klein.php b/src/Klein/Klein.php index 1953ed77..6b0bdea2 100644 --- a/src/Klein/Klein.php +++ b/src/Klein/Klein.php @@ -341,6 +341,44 @@ public function respond($method, $path = '*', $callback = null) return $route; } + /** + * Middleware alternative for respond() that doesn't count as a + * matched route. + * + * + * $router = new Klein(); + * + * $router->filter( function() { + * echo 'this works'; + * }); + * $router->filter( '/endpoint', function() { + * echo 'this also works'; + * }); + * $router->filter( 'POST', '/endpoint', function() { + * echo 'this also works!!!!'; + * }); + * + * + * @param string $method + * @param string $path + * @param callable $callback + * @return Route + */ + public function filter($method, $path = '*', $callback = null) + { + // Get the arguments in a very loose format + extract( + $this->parseLooseArgumentOrder(func_get_args()), + EXTR_OVERWRITE + ); + + $route = $this->route_factory->build($callback, $path, $method, false); + + $this->routes->add($route); + + return $route; + } + /** * Collect a set of routes under a common namespace * diff --git a/src/Klein/RouteFactory.php b/src/Klein/RouteFactory.php index 8c218bf4..8ff67afa 100644 --- a/src/Klein/RouteFactory.php +++ b/src/Klein/RouteFactory.php @@ -128,7 +128,7 @@ public function build($callback, $path = null, $method = null, $count_match = tr $callback, $this->preprocessPathString($path), $method, - $this->shouldPathStringCauseRouteMatch($path) // Ignore the $count_match boolean that they passed + ($this->shouldPathStringCauseRouteMatch($path) && $count_match) // actually pass in the count_match flag ); } }