feat($routeProvider): add support for regular expressions routes #5542
feat($routeProvider): add support for regular expressions routes #5542VTJamie wants to merge 3 commits into
Conversation
There was a problem hiding this comment.
This could potentially be an array of values instead of individual properties. Was unsure of the prefered convention.
|
I'm sorry, but I wasn't able to verify your CLA signature. CLA signature is required for any code contributions to AngularJS. Please sign our CLA and ensure that the CLA signature email address and the email address in this PR's commits match. If you signed the CLA as a corporation, please let me know the company's name. Thanks a bunch! PS: If you signed the CLA in the past then most likely the email addresses don't match. Please sign the CLA again or update the email address in the commit of this PR. |
|
Just Submitted the CLA |
|
CLA signature verified! Thank you! Someone from the team will now triage your PR and it will be processed based on the determined priority (doc updates and fixes with tests are prioritized over other changes). |
|
Hi, Thanks! |
add regular expression routes with generic param binding to groups
There was a problem hiding this comment.
Added Unit test to verify that standard routes, routes with params, regex routes without params, and regex routes with params work
|
Example use. $routeProvider.when('/route', {controller: angular.noop, reloadOnSearch: false}); //Regular route |
|
I'm wondering if it might better to do something more like ui-router's url format documented here, where parameters can be in a regexp format if necessary. I can see that being helpful to people, but I'm not totally sure I understand the net benefit of using a regexp for a route url. |
|
This is an attempt to provide the most power possible, with the smallest change possible. It is also designed to be a completely take it or leave it feature. Also without needing to change the developer's current implementation of $routeProvider over to $stateProvider. |
|
@caitp : I implemented something similar to what you want. So you can define things like: Which will only match those routes where Important: Even though that regular expression in first case ends with What were the changes that I did? I'm not making a pull request for this as it seem too tedious, but it's a simple code addition to route.js file Replace these lines: with these: All the usual stuff applies:
|
|
Any update on this? I'd really like to see it merged. |
|
-1 params in the format @VTJamie you haven't explained in what real world scenarios would this feature be useful. I can think of some exotic corner cases, in which I'd rather rethink the route patterns than try to create complex regular expressions to match them. @litera's implementation is more interesting and he provides the example of parsing the route param into isbn10 or isbn13 depending on the pattern. But even in this case, I'd move the responsibility of determining the isbn format to the controller rather than the router. @btford @EisenbergEffect what's your take on defining routes via regexps instead of pattern strings? |
|
As an example, consider a case of browsing through a collection of collections, for example a website of photo galleries. The site may have the URL structure but in going to you just start off at the most recently added photo. Without the literal string That kind of thing would be trivial with a RegEx, looking something like |
|
(That's not to say this is the right PR to add that feature, only that I think this feature is worth having) |
|
Well, I'm not personally a fan of RexEx. So, I've got a bias here. I feel that they introduce a bit of a maintenance issue since RegEx can tend to be "write only" code. It takes considerably more mental effort for a developer to process what a RegEx is doing if they didn't just finish writing it ;) That said, it's important not to block developers from getting their job done, so if there's a legitimate scenario that cannot be modeled by the simple route syntax, then I think it's worth it to try to find some solution. I'm much less familiar with the 1.x router, so I won't speak on that, but I can talk a bit about how this relates to the 2.0 router we've been working on, for those who are interested. First, we're currently using the RouteRecognizer library to do our pattern matching. It's a solid library with a consistent set of behavior. A side effect of this library is that it does not represent route patterns via RegEx internally. Rather, it builds up its own state machine for matching. So, our first barrier (well, not really, as you'll see below) in 2.0 is that our underlying matcher doesn't support raw RegEx. It does support wildcard routes, which I think could be used in some fashion to solve this problem though. However, the 2.0 router is very flexible and it supports an "unknown route handler". Whenever a route is not matched via the standard mechanism above, we look to see if you've registered this custom handler. If you have, we pass you the raw route data and expect you to build up what we call a NavigationInstruction. You can build this instruction up any way you like, including RegEx. You can even asynchronously call to the server to get the data you need. When you are finished, you hand it back to us and the Router uses it to process the navigation. So, it is at this level that one could plug in any RegEx strategy that they desired and teach the router how to handle that. I'm aware that the solution for the 2.0 router I present here isn't as simple as declaring a RegEx route. But I think it's important to make the 80%-90% use case dead simple and the other cases "possible". So, I believe we have achieved that. I should also make another note on why it's built this way. The 2.0 router supports nested child routers and sibling routers. We also support a lot of internal smarts that determine when/how to transition state in the context of these hierarchical route scenarios. As a result, it's important that the route info is not a black box. We usually need to look at that and determine other information during the course of routing. Supporting arbitrary RegEx would make this very difficult. This is why we actually give you the hook to generate a NavigationInstruction, as mentioned above. The instruction provides a query surface for the router's internal pipeline that allows it to gain access to the information it needs to transition between states. As a developer, having knowledge of your own RegEx, you can then provide us the information we require. Finally, since I'm talking about 2.0 here....none of this is set in stone. You can take a look at our current implementation here. It's still far from complete, but there's enough there for you to get a pretty solid feel for where we are going with this. I always welcome feedback...and if you've got some cool ideas about supporting RegEx in that implementation, I'd love to hear them. |
02dc2aa to
fd2d6c0
Compare
cad9560 to
f294244
Compare
e8dc429 to
e83fab9
Compare
4dd5a20 to
998c61c
Compare
|
sooooo, it's been a long time since this was proposed, and it sounds like Rob and Igor weren't keen on it, plus it's rather bitrotten now. @btford maybe it's something you might want to look at for the new router, but we should definitely try to keep things as simple as possible imho. Closing this for now, sorry :( |
|
No worries :) |
I had a requirement for doing a pure regexp route with parameter grabbing. I attempted to choose a generic naming using regexp_param(n). The original path string sterilization is wrapped in an if condition to determine if it is a regular expression.