Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[Routing] Allow inline definition of requirements and defaults#26518

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged

Conversation

@nicolas-grekas
Copy link
Member

@nicolas-grekasnicolas-grekas commentedMar 14, 2018
edited by javiereguiluz
Loading

QA
Branch?master
Bug fix?no
New feature?yes
BC breaks?no
Deprecations?no
Tests pass?yes
Fixed tickets#26481
LicenseMIT
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 traditionaldefaults syntax for that)
  • ...

linaori, derrabus, andreybolonin, kunicmarko20, sstok, sadikoff, nibsirahsieu, GwendolenLynch, ostrolucky, Destroy666x, and 9 more reacted with thumbs up emoji
$this->assertEquals((newRoute('/foo/{bar}'))->setRequirement('bar','\d+'),newRoute('/foo/{bar<.*>}',array(),array('bar' =>'\d+')));

$this->assertEquals((newRoute('/foo/{bar}'))->setDefault('bar',null)->setRequirement('bar','.*'),newRoute('/foo/{bar<.*>?}'));
$this->assertEquals((newRoute('/foo/{bar}'))->setDefault('bar','<>')->setRequirement('bar','>'),newRoute('/foo/{bar<>>?<>}'));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

This one is surprising. Instead of/foo/{bar<>>?<>} I thought this should be like/foo/{bar<\>>? <>} to escape the> value as a requirement.

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

there is no escaping to keep the parser trivial
I don't think this limits anything in anyway

javiereguiluz reacted with thumbs up emoji
@derrabus
Copy link
Member

Regarding the escaping: Could my default value contain curly braces?

@nicolas-grekas
Copy link
MemberAuthor

nicolas-grekas commentedMar 14, 2018
edited
Loading

Could my default value contain curly braces?

Not a closing one that's correct.

@javiereguiluzjaviereguiluz added the ❄️ Feature FreezeImportant Pull Requests to finish before the next Symfony "feature freeze" labelMar 14, 2018
@derrabus
Copy link
Member

All right. We can probably live with that limitation, but we should document it.

@Tobion
Copy link
Contributor

I would also leave escaping out. If you have such a special regex or default, you can simply use the old long route definition.

javiereguiluz and derrabus reacted with thumbs up emoji

@ostrolucky
Copy link
Contributor

will !php/const work inside?

jaikdean reacted with thumbs up emoji

@nicolas-grekas
Copy link
MemberAuthor

will !php/const work inside?

no, but you could submit a PR on top to make constants work, eg

{bar!SOME_CONST} -- no requirement, with default value{bar<.*>!SOME_CONST} -- with requirement and default value

@nicolas-grekas
Copy link
MemberAuthor

nicolas-grekas commentedMar 14, 2018
edited
Loading

you could submit a PR on top to make constants work, eg

or just use the existing syntax btw... (explicitdefaults: ...)

javiereguiluz reacted with thumbs up emoji

@javiereguiluz
Copy link
Member

Is it really so important to support PHP constants here? They could make routes really difficult to understand or very easy to override some typo or wrong character.

@Destroy666x
Copy link

Destroy666x commentedMar 14, 2018
edited
Loading

Nice feature. What about a case like{bar<test>?>?test}?

@nicolas-grekas
Copy link
MemberAuthor

nicolas-grekas commentedMar 14, 2018
edited
Loading

@Destroy666x see tests, that's covered: the shortest regexp is used
(use e.g.{bar<test[>]?>?test} if you want the other way)

@ostrolucky
Copy link
Contributor

It's not so important, was just curious. Seems this syntax can be freely combined with _defaults anyway 👍

@Destroy666x
Copy link

Destroy666x commentedMar 14, 2018
edited
Loading

@nicolas-grekas I see a similar test but without? inside<>, which is allowed in requirements, right? It's kind of ambigous which? is the separator

EDIT: I see, could you please add a test forbar<test[>]?>?test too then? Or maybe it's good to mention that in documentation

Copy link
Contributor

@ostroluckyostrolucky left a comment
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I don't think this is gonna work. Route::__construct calls setPath first, then setDefaults which erases those defaults as a first thing

@nicolas-grekas
Copy link
MemberAuthor

nicolas-grekas commentedMar 14, 2018
edited
Loading

I don't think this is gonna work. Route::__construct calls setPath first, then setDefaults which erases those defaults first

please review again, addDefaults is used ;)

@ostrolucky
Copy link
Contributor

Indeed. Can you add a test case for passing defaults both via path and via defaults tho?

@nicolas-grekas
Copy link
MemberAuthor

Indeed. Can you add a test case for passing defaults both via path and via defaults tho?

review again :)

ostrolucky reacted with thumbs up emojiostrolucky reacted with laugh emoji

{
$this->assertEquals((newRoute('/foo/{bar}'))->setDefault('bar',null),newRoute('/foo/{bar?}'));
$this->assertEquals((newRoute('/foo/{bar}'))->setDefault('bar','baz'),newRoute('/foo/{bar?baz}'));
$this->assertEquals((newRoute('/foo/{bar}'))->setDefault('bar','baz<buz>'),newRoute('/foo/{bar?baz<buz>}'));
Copy link
Contributor

@TobionTobionMar 14, 2018
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

How about we make it so that it does not matter if you specify a default or regex first?
So'/foo/{bar?baz<.*>}' == '/foo/{bar<.*>?baz}'

This seems more user-friendly as you don't need to remember the order. Also this syntax is not feature-complete anyway due to missing escaping. So if you really need to specify a default likebaz<buz> then just use the explicit config. But having such a default is much less likely than a user mixing up the order.

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Copy link
MemberAuthor

@nicolas-grekasnicolas-grekasMar 14, 2018
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I think having two ways to express things just creates more confusion.
Having only one order means everyone will write these in exactly the same way, thus less WTF when switching projects.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Normally I agree consistency is better. But I also want to prevent bugs in the first place. So when we do not accept both ways, it would be cleaner to throw an exception if you do it wrong. I cannot believe somewhat actually uses a default likebaz<buz> in a URL.

Copy link
MemberAuthor

@nicolas-grekasnicolas-grekasMar 16, 2018
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I cannot believe somewhat actually uses a default like baz in a URL

This is not a reason to technically forbid it. Symfony devs have been proved surprising many time :)

Copy link
Contributor

@TobionTobionMar 16, 2018
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

It's not forbidden. You just need to use the explicit syntax.

Symfony devs have been proved surprising many time

Yes they will be surprised with unexpected behavior.

publicfunctionsetPath($pattern)
{
if (false !==strpbrk($pattern,'?<')) {
$pattern =preg_replace_callback('#\{(\w++)(<.*?>)?(\?[^\}]*+)?\}#',function ($m) {
Copy link
Contributor

@ro0NLro0NLMar 15, 2018
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

what does{bar<>} exactly do?

ah that's sanitized. never mind.. perhaps a test?

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

this is already tested elsewhere, nothing specific to this feature

@nicolas-grekas
Copy link
MemberAuthor

@symfony/deciders votes pending


$this->assertEquals((newRoute('/foo/{bar}'))->setDefault('bar',null)->setRequirement('bar','.*'),newRoute('/foo/{bar<.*>?}'));
$this->assertEquals((newRoute('/foo/{bar}'))->setDefault('bar','<>')->setRequirement('bar','>'),newRoute('/foo/{bar<>>?<>}'));
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

maybe there should also be a test case with using curly braces in the requirements?

Like'/foo/{_locale<[a-z]{2}>}' for example?

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

case added

dmaicher reacted with thumbs up emoji
@nicolas-grekasnicolas-grekas merged commit67559e1 intosymfony:masterMar 19, 2018
nicolas-grekas added a commit that referenced this pull requestMar 19, 2018
…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
@nicolas-grekasnicolas-grekas deleted the route-inline-reqs branchMarch 19, 2018 12:13
@fabpotfabpot mentioned this pull requestMay 7, 2018
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@javiereguiluzjaviereguiluzjaviereguiluz left review comments

@derrabusderrabusderrabus approved these changes

+5 more reviewers

@ro0NLro0NLro0NL left review comments

@ostroluckyostroluckyostrolucky approved these changes

@TobionTobionTobion approved these changes

@dmaicherdmaicherdmaicher approved these changes

@SimperfitSimperfitSimperfit approved these changes

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Labels

FeatureRouting❄️ Feature FreezeImportant Pull Requests to finish before the next Symfony "feature freeze"Status: Reviewed

Projects

None yet

Milestone

4.1

Development

Successfully merging this pull request may close these issues.

10 participants

@nicolas-grekas@derrabus@Tobion@ostrolucky@javiereguiluz@Destroy666x@dmaicher@ro0NL@Simperfit@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp