how to get which route is run?
Let's say I have twenty routes in my project, and I want to log every request with the route. Currently, I do
$bramus->all("/user/{id}", function () {
logRequest("/user/{id]")
}
Of course this is grossly oversimplified. I would like to not have to send the route as a parameter and get it from bramus/router. I added $this->runRoutes[] = $route["pattern"]; to the handle() function and this is enough at the moment but I would like to know if there is already a way to get a list of matching (and running) routes from bramus/router.
For the first part of your question (the logging part):
Don't forget that the current URI can be found in $_SERVER['REQUEST_URI']. Combined with a Before Router Middleware you can log all requests.
Something like this:
$router->before('GET|POST|PUT|DELETE|PATCH|OPTIONS', '.*', function() use ($logger) {
$logger->log($_SERVER['REQUEST_URI']);
}
For the second part of your question (to know which route has been run): I can look into this, or will happily accept a PR that provides this feature.
Reopening, didn't get into the 2nd part of the question … oops 😊
I kinda hacked up Bramus slightly to do just this; return the routes that run rather than execute the callback handlers. The run function returns an array of Route objects, which from there you can handle as you need. In my instance, I needed to be able to have each call back class created by my dependency injector (Auryn). I could submit a pr, but I need to change a few things first as I altered the handler method to no longer function as expected.
To get the router to return the routes rather than execute the callbacks, it could set a simple boolean.
$router = new \Bramus\Router\Router();
$router->setReturnRoutes(true);
// Define routes as normal
$matches = $router->run();
$matches would contain the method, pattern and callback of any/all routes that matched, including before middleware. From this point, you could handle as you see fit. If the interest and willingness to add the feature is there, I'll gladly submit the PR.