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

[HttpFoundation] Adds getAcceptableFormats() method for Request#26486

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

Conversation

@AndreiIgna
Copy link

@AndreiIgnaAndreiIgna commentedMar 11, 2018
edited
Loading

QA
Branch?master
Bug fix?no
New feature?yes
BC breaks?no
Deprecations?no
Tests pass?yes
Fixed tickets#21909
LicenseMIT
Doc PRsymfony/symfony-docs#...

Adds a new methodgetAcceptableFormats() forRequest. This reads the content types in Accept header and maps them to formats already defined inRequest class

In a request made by a browser, based on the default Accept header, thegetAcceptableFormats() will return an array['html', 'xml']

In progress

  • gather feedback for my changes
  • submit changes to the documentation

sstok and andreybolonin reacted with thumbs up emoji
@derrabus
Copy link
Member

It looks like this PR contains unrelated commits. Could you do a rebase against master?

@AndreiIgnaAndreiIgnaforce-pushed thehttpfoundation-request-add-acceptable-formats branch from08a9337 to00acc8cCompareMarch 13, 2018 12:15
@AndreiIgna
Copy link
Author

Oops used GitHub desktop, which did something unexpected to the rebase. Should be ok now

derrabus reacted with thumbs up emoji

protected$format;

/**
* @var string
Copy link
Member

Choose a reason for hiding this comment

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

Should bearray.

* The`get()` method of the`AcceptHeader` class now takes into account the
`*` and`*/*` default values (if they are present in the Accept HTTP header)
when looking for items.
* Adds`getAcceptableFormats()` for reading acceptable formats based on Accept header
Copy link
Member

Choose a reason for hiding this comment

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

Should beadded to be consistent with the wording of other entries in that file.

@AndreiIgna
Copy link
Author

Thanks noticing and letting me know about those@derrabus

The added method makes it easier to check what data formats the clients accepts, ex:

if (in_array('html', $request->getAcceptableFormats())) {  // return html data, maybe a template} elseif (in_array('xml', $request->getAcceptableFormats())) {  // return xml} else {  // return json data}

If this is useful and included in next release, I'll make the addition in documentation repo as well

return$this->acceptableFormats;
}

return$this->acceptableFormats =array_values(array_unique(array_filter(array_map(array($this,'getFormat'),$this->getAcceptableContentTypes()))));

Choose a reason for hiding this comment

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

isarray_values actually required?

Copy link
Author

Choose a reason for hiding this comment

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

Is added because the mix ofarray_filter andarray_unqiue sometimes returns[0 => 'html', 2 => 'xml', 5 => 'json'].
This create problems with tests or confusion when checking the array, so it's best to reset the keys

derrabus reacted with thumbs up emoji
*
* @return array List of acceptable formats by the client
*/
publicfunctiongetAcceptableFormats()
Copy link
Member

@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.

missing return type (and removal of the corresponding docblock line since it will become useless)

derrabus and AndreiIgna reacted with thumbs up emoji
@AndreiIgnaAndreiIgnaforce-pushed thehttpfoundation-request-add-acceptable-formats branch 2 times, most recently from1e9bf3f toeef572bCompareMarch 23, 2018 09:38
@nicolas-grekasnicolas-grekas modified the milestones:4.1,nextApr 20, 2018
@nicolas-grekas
Copy link
Member

@AndreiIgna would you mind rebasing and moving the line in the changelog on 4.2?

@AndreiIgnaAndreiIgnaforce-pushed thehttpfoundation-request-add-acceptable-formats branch fromeef572b to7f062b1CompareJune 5, 2018 08:28
@AndreiIgna
Copy link
Author

Yes sure, is updated now

/**
* @var array
*/
protected$acceptableFormats;
Copy link
Member

Choose a reason for hiding this comment

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

should be private

4.2.0
-----

* Adds`getAcceptableFormats()` for reading acceptable formats based on Accept header
Copy link
Member

Choose a reason for hiding this comment

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

added

@AndreiIgnaAndreiIgnaforce-pushed thehttpfoundation-request-add-acceptable-formats branch from7f062b1 to9bfb693CompareJune 11, 2018 10:52
@AndreiIgna
Copy link
Author

Updated based on last review.

While checking in more detail the similar$acceptableContentTypes, saw that it's assigned null oninitialize andduplicate methods. Added this for$acceptableFormats too, hope this is ok

@nicolas-grekasnicolas-grekasforce-pushed thehttpfoundation-request-add-acceptable-formats branch from9bfb693 to8a127eaCompareJune 19, 2018 11:26
@nicolas-grekas
Copy link
Member

Thank you@AndreiIgna.

@nicolas-grekasnicolas-grekas merged commit8a127ea intosymfony:masterJun 19, 2018
nicolas-grekas added a commit that referenced this pull requestJun 19, 2018
…r Request (AndreiIgna)This PR was squashed before being merged into the 4.2-dev branch (closes#26486).Discussion----------[HttpFoundation] Adds getAcceptableFormats() method for Request| Q             | A| ------------- | ---| Branch?       | master| Bug fix?      | no| New feature?  | yes| BC breaks?    | no| Deprecations? | no| Tests pass?   | yes| Fixed tickets |#21909| License       | MIT| Doc PR        | symfony/symfony-docs#... <!-- required for new features -->Adds a new method `getAcceptableFormats()` for `Request`. This reads the content types in Accept header and maps them to formats already defined in `Request` classIn a request made by a browser, based on the default Accept header, the `getAcceptableFormats()` will return an array `['html', 'xml']`In progress- [x] gather feedback for my changes- [x] submit changes to the documentationCommits-------8a127ea [HttpFoundation] Adds getAcceptableFormats() method for Request
javiereguiluz added a commit to symfony/symfony-docs that referenced this pull requestJun 19, 2018
…od (AndreiIgna)This PR was squashed before being merged into the master branch (closes#9898).Discussion----------[HttpFoundation] Add info for getAcceptableFormats() methodAdds info for `Request::getAcceptableFormats()`Should it add more info on docs page? Ex: `Request::getAcceptableContentTypes` returns the accepted content types which can be `text/html`, `application/json`, etc.`Request::getAcceptableFormats` processes that and returns the formats based on those content types `html`, `json`refsymfony/symfony#26486Commits-------65d100e [HttpFoundation] Add info for getAcceptableFormats() method

$request =newRequest();
$request->headers->set('Accept','text/html, application/xhtml+xml, application/xml;q=0.9, */*');
$this->assertEquals(array('html','xml'),$request->getAcceptableFormats());
Copy link
Contributor

Choose a reason for hiding this comment

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

why would it only return html and xml if everything is acceptable according to*/*? I think this shows that the method is not clear and generic enough to be added to the core IMO.

Copy link
Contributor

Choose a reason for hiding this comment

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

I can't see where I would possibly use this method. What would be useful is a similar method that accepts formats as argument and would return the ones that are acceptable according to the accept header. This would then allow to make use of*/* and this is what people usually need in REST APIs etc.

jvasseur and plandolt reacted with thumbs up emoji
@nicolas-grekas
Copy link
Member

FYI, reverted in#29047

nicolas-grekas added a commit that referenced this pull requestNov 1, 2018
…ethod for Request" (Tobion)This PR was merged into the 4.2-dev branch.Discussion----------Revert "[HttpFoundation] Adds getAcceptableFormats() method for Request"This reverts commit8a127ea.| Q             | A| ------------- | ---| Branch?       | master| Bug fix?      | no| New feature?  | no <!-- don't forget to update src/**/CHANGELOG.md files -->| BC breaks?    | no     <!-- seehttps://symfony.com/bc -->| Deprecations? | no <!-- don't forget to update UPGRADE-*.md and src/**/CHANGELOG.md files -->| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->| Fixed tickets |#26486| License       | MIT| Doc PR        |As I said in#26486 (comment) and people wonder inhttps://symfony.com/blog/new-in-symfony-4-2-acceptable-request-formats#comment-22747, I don't think this method clear and generic enough to be added to the core.I can't see where I would possibly use this method. What would be useful is a similar method that accepts formats as argument and would return the ones that are acceptable according to the accept header. This would then allow to make use of `*/*` and this is what people usually need in REST APIs etc.But for now, we should revert it before it gets released like this.Commits-------397ed83 Revert "[HttpFoundation] Adds getAcceptableFormats() method for Request"
@nicolas-grekasnicolas-grekas modified the milestones:next,4.2Nov 1, 2018
This was referencedNov 3, 2018
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@fabpotfabpotfabpot left review comments

@derrabusderrabusderrabus requested changes

@javiereguiluzjaviereguiluzjaviereguiluz approved these changes

@nicolas-grekasnicolas-grekasnicolas-grekas approved these changes

+1 more reviewer

@TobionTobionTobion left review comments

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Projects

None yet

Milestone

4.2

Development

Successfully merging this pull request may close these issues.

7 participants

@AndreiIgna@derrabus@nicolas-grekas@fabpot@javiereguiluz@Tobion@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp