nikic / FastRoute

Fast request router for PHP
Other
5.1k stars 445 forks source link

Active route pattern #225

Closed Jackson88 closed 7 months ago

Jackson88 commented 3 years ago

Hello, kinda question: is it possible to define the active route pattern itself, for example:

$dispatcher = FastRoute\simpleDispatcher(function(FastRoute\RouteCollector $r) {
    $r->addRoute('GET', '/user/{id:\d+}', 'get_user_handler');
});
....

$routeInfo = $dispatcher->dispatch($httpMethod, $uri);
$activePattern = $routeInfo[3]; // '/user/{id:\d+}'

Regards.

burzum commented 3 years ago

I'm not sure what you mean? You want to be able to get the matched pattern in the result?

Jackson88 commented 3 years ago

Exactly, be able to identify used route "rule", for general purposes like statistic/analytics

burzum commented 3 years ago

@Jackson88 the router builds a regex-map internally I don't think the current version allows you to identify the used route declaration but I'm working towards being able to do so in #233

burzum commented 3 years ago

@Jackson88 you can just use Google Analytics or the PSR request object and it's URL itself to generate statistics. I don't see how a route itself gives you good statistics.

kktsvetkov commented 2 years ago

@Jackson88 can you use the handler instead of the route to track the states? In other words, instead of tracking how many times the route was matched, you are going to track how many times the handler was returned as result. In most cases, if there are no duplicate routes for the same handler, it should do the job.

kktsvetkov commented 2 years ago

In Symfony Routing there is this feature called "Extra Parameters", where when you declare a route you not only assign a handler to it, but you can also attach some extra parameters that will be included in the results if the route is matched. I think under the hood this is what Symfony Routing is using to remember the route names as well.

I think such a feature here will help both with the named routes, as well as with any other baggage that you want to pass along with a route that has been matched.

WilliamStam commented 1 year ago

"extra parameters" would be really cool. that would make life SOOO much easier with auth middleware.