Skip to content

Add a way to find "best match" for content-type based on Accept header #21909

@anlutro

Description

@anlutro
Q A
Bug report? no
Feature request? yes
BC Break report? no
RFC? yes
Symfony version 3.x

In Python, the framework Flask has a method on its AcceptHeaders equivalent called best_match which basically compares the Accept header with your list of available content-types and returns the best match.

I found myself implementing this manually in an abstract controller class:

foreach ($request->getAcceptableContentTypes() as $type) {
	if ($type == 'application/json') { return $type; }
	if ($type == 'text/plain') { return $type; }
	if ($type == 'text/html') { return $type; }
}
return 'text/plain';

Is this something that could be added to the AcceptHeaders class?

PS: I wasn't able to find any info on what RFC yes/no entails so I might have put the wrong thing there.

(edit by @lyrixx: Yes, it's a RFC, I fixed the PR description. Thanks)

Metadata

Metadata

Assignees

No one assigned

    Labels

    FeatureHttpFoundationRFCRFC = Request For Comments (proposals about features that you want to be discussed)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions