azjezz / hack-routing

A PHP rewrite of HackRouter by Facebook
MIT License
26 stars 0 forks source link

Path generation from string pattern #2

Closed dominikzogg closed 3 years ago

dominikzogg commented 3 years ago

Is your feature request related to a problem? Please describe. No problem,

Describe the solution you'd like I would love to be able generate a path from a pattern and an array for the variable parts. Something like: https://github.com/chubbyphp/chubbyphp-framework-router-hack-routing/blob/master/src/UrlGenerator.php

Describe alternatives you've considered The alternative would be keep the custom implementation as mentioned in describe the solution

Additional context Add any other context or screenshots about the feature request here.

azjezz commented 3 years ago

This is something a bit tricky, it's not supported by hack-router, and unlikely to be added here first.

currently, there's 2 ways to do this, but 1 is not possible with "string" pattern, and the other is complicated and slow.

  1. use UriPattern instead of AbstractRouter/Router

see: https://github.com/azjezz/hack-routing/blob/main/examples/UriPatternsExample.php

  1. store all routes in an array, and to generate the path for a route, get the route from that array, parse it again ( probably cacheable step ), and uri builder.

I already did this in hacklang, which you can see here: https://github.com/nuxed/http-router/blob/develop/src/Nuxed/Http/Router/Generator/UriGenerator.hack, but this code is old, slow, and probably not perfect, so you might need to optimize it a bit.

dominikzogg commented 3 years ago

Similar to what I did in the given link. Thanks!

azjezz commented 3 years ago

you should probably use the same cache to cache the parsed data. as that step is usually slow.