-
-
Notifications
You must be signed in to change notification settings - Fork 10.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Customizable URL matching rules #2286
Comments
If this were a small, standalone lib I think I would gladly consider using At one point we had this idea to do paramTypes, similar to React's On Thu, Oct 15, 2015 at 1:58 PM Jimmy Jia notifications@github.com wrote:
|
That makes sense. I might have some time to look at this in a week or so once I get some work deadlines out of the way. Any thoughts on a reasonable syntax that's compatible with the current syntax for URL parameters? |
The syntax you have here works well. If something is between <> we know
On Thu, Oct 15, 2015 at 3:08 PM Jimmy Jia notifications@github.com wrote:
|
We have a new syntax proposal from @th0r regarding the syntax, I personally like it more, as it doesn't require a more advanced DSL than the one that is currently supported and it's more readable (at least in my opinion). Here is an example: const params = {
messageId: string({ length: 5 }),
size: enum(['small', 'medium', 'big'])
};
const routes = (
/* Common params for the whole app */
<Route params={params}>
<Route path="pizza/:size" component={Pizza}/>
<Route path="message/:messageId" component={Message}/>
/**
* We are overriding `size` parameter description for the whole routes branch here:
* `params` object will be merged with the parent's route `params` here
*/
<Route path="clothes" component={Clothes} params={{ size: enum(['S', 'M', 'L', 'XL']) }}>
<Route path="tshirt/:size" component={Tshirt}/>
/* We can override params per-route if we really need it */
<Route path="tshirt/big/:size" component={BigTshirt} params={{ size: enum(['XXL', 'XXXL']) }}/>
</Route>
</Route>
); The params attribute can be added to the route to identify the rules for the parameters. This allows to define all the parameter rules at the root level or at the specific route, and any mixing of the two approaches. |
Here is an almost finished WIP https://github.com/itajaja/url-matcher I am not adding it to npm or to Travis, if you decide to port the code to rackt and someone else wants to take ownership. I changed some of the APIs to look a little bit more consistent and robust with rules, and stated clearly what do they return if e.g. a route doesn't match. Let me know what you guys think |
I would love to see this added. I need to handle routes like "/ca/los-angeles/...", for all states, along side non-state code routes like "/:category/:topic"? I'd like to avoid adding 50 routes, but I know that would work. |
We've been a little swamped with getting 1.0.0 out the door and dealing with the immediate aftermath. I'd definitely like to work on getting this integrated when I have a bit more time, though. |
@itajaja Do you have an idea how url-match would connect with react-router? Specifically how would react-router const routes = (
<Route name="home" path="/" component={RootComponent}>
<Route name="recordList" /*path=""*/ customMatcher={matchPattern()} component={RecordList} />
<Route name="record" /*path=""*/ customMatcher={matchPattern()} component={Record} />
</Route>
); |
what about import * as matcher from 'url-matcher'
<Router matcher={matcher}>
<Route path="user/:id" component={Pizza} params={{ id: int(max: 8) }}/>
</Router> Where In this case, |
Why not make it all pluggable? We ship our implementation as the default, but anybody can go nuts:
It could then pass |
I think for now the best way to go is to do something like with So by default you'd just get a |
Or, if that's too much to ask, maybe we just provide a function on a route instead of a path that returns true or false:
|
Potentially it could make sense to do both - allow specifying something like a function for This would allow routes to fail to match by calling a |
+1 for
This is something very obvious I expected the router to have |
The problem is that a matcher function is not enough – it's break |
Throwing this out there back in my PHP days.... Symfony2's routing just seemed "right" http://symfony.com/doc/current/book/routing.html#adding-requirements having a "defaults" and "requirements" property |
Any updates on this issue? Currently I'm forced to overload history's |
See #3105. |
Folded into #3611. |
This isn't really a limitation of 4.0.0, right? I'm assuming we could just make custom |
@eloiqs I don't understand how that relates to what I was saying. Though ambiguous matches are cool |
@jedwards1211 I may have read this post a bit fast, but I was under the impression that we may be able to implement the kind of logic necessary for matching some URL parameters to specific rule sets by doing something like the example I linked! sorry to bother 🙈 |
Lifting this from #2284:
@mjackson
Sure - what I mean is that Flask/Werkzeug offer an example of a good abstraction of pluggable pattern matching (via a mini-DSL) that has worked pretty well for me. The way it looks like is that instead of e.g. our
The preferred syntax for specifying a URL rule looks like e.g.
This is extensible to allow people to do things like
str
,int
andpath
are built-in (in this case the URL rules handle both matching and converting params), whilemy_custom_rule
is an example of the syntax for using a custom URL matching rule. Per thestr(length=2)
example these converters can also take arguments.These rules are used to both match and generate URLs.
The nice thing about something like this sort of general solution (not necessarily in this exact syntax form) is that they'd let users solve their own problems, while still keeping the same workflow around route matching, instead of adding something like
next()
from Express.ETA: Werkzeug docs are here: http://werkzeug.pocoo.org/docs/0.10/routing/
The
<int:foo>
case is one decent motivation for having something like this BTW - I've found it a nice convenience to have my URL routing framework just 404 anything that doesn't match the pattern I want before it even gets to my URL handler, and if I'm specifying that it's an integer in the URL rule, I may as well also have the routing convert that parameter to the right type (rather than just a string).Also I forgot to say, but the difference between
str
andpath
above is thatpath
is like the**
glob in that it also matches/
s as needed.The text was updated successfully, but these errors were encountered: