-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[DX] Improve route requirements definition: allow to define requirements inline with placeholders itself #26481
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
Comments
For reference purposes, Django framework allows something similar: 'articles/<int:year>/<int:month>/<slug:slug>/' The special converters are r'^articles/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/$' |
It's been on my list of things to do forever. I would love to see somebody work on this one. Would definitely be a huge DX improvement. |
We can consider the previous comment from Fabien as an approval of this idea 🎉, so now let's talk about the details before someone can try to implement it. The syntax proposed by @Strate looks nice to me:
I'd use that simple syntax and wouldn't complicate things with something like the Django's converters. Now, two things to think about:
|
How about using a The |
Just throwing out some ideas here, because I really love this proposal:
|
+1 for this. Remind me a few years back when i was using play framework. Just for reference https://www.playframework.com/documentation/2.6.x/ScalaRouting#dynamic-parts-with-custom-regular-expressions |
What about this?
I think the implementation shouldn't be very complex, we could put the parsing logic in Anyone willing to give it a try? |
I can try :) |
I like @nicolas-grekas syntax the most. But I would propose a small change
I think this looks more what people are used to and makes it clear what the name and what the value is. I would also recommend to do the parsing directly in |
One problem I see with having the default value inside the path is that it will be hard to set a non-string default. Often times you only want to make the param optional but don't care about the default. You just need to be able to distinguish between a given value and no value given. For example this common case
Translating that to
For this common case I would propose to add a shortcut syntax
|
Actually that brings be back to what nicolas proposed. With a little addition: You can leave out the default value, which means
|
What will happen with something like: |
The ? just configures the default value. Being optional means both having a default value and being at a tail position. Nothing new here. |
I like Nicolas proposal of using
And now some real-world route examples to see this in action:
|
I agree that in a real world example, it looks a lot better with |
Maybe both of these cases should be made valid. So You don't have to remember which way it is....
|
|
Here we are: #26518
The default value is a string, why should it be parsed? Not a good idea to me as it creates more ambiguities than it should.
because |
OK. I just wanted to avoid issues if some day Symfony adopts the URI Template RFC, where |
…defaults (nicolas-grekas) This PR was merged into the 4.1-dev branch. Discussion ---------- [Routing] Allow inline definition of requirements and defaults | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #26481 | License | MIT | Doc PR | - ``` {bar} -- no requirement, no default value {bar<.*>} -- with requirement, no default value {bar?default_value} -- no requirement, with default value {bar<.*>?default_value} -- with requirement and default value {bar?} -- no requirement, with default value of null {bar<.*>?} -- with requirement and default value of null ``` Details: * Requirements and default values are not escaped in any way. This is valid -> `@Route("/foo/{bar<>>?<>}")` (requirements = `>` and default value = `<>`) * Because of the lack of escaping, you can't use a closing brace (`}`) inside the default value (wrong -> `@Route("/foo/{bar<\d+>?aa}bb}")`) but you can use it inside requirements (correct -> `@Route("/foo/{bar<\d{3}>}")`). * PHP constants are not supported (use the traditional `defaults` syntax for that) * ... Commits ------- 67559e1 [Routing] Allow inline definition of requirements and defaults
It would be great to inline requirements to pattern itself:
/route/path/{parameter:\d+}
instead of/route/path/{parameter}, requirements={{parameter: "\d+"}}
The text was updated successfully, but these errors were encountered: