final class EarlyFormatSetter extends RequestFormatRouteFilter (View source)

internal  
 

Sets the 'api_json' format for requests to JSON:API resources.

Because this module places all JSON:API resources at paths prefixed with /jsonapi, and therefore not shared with other formats, \Drupal\Core\Routing\RequestFormatRouteFilter does correctly set the request format for those requests. However, it does so after other filters, such as \Drupal\Core\Routing\ContentTypeHeaderMatcher, run. If those other filters throw exceptions, we'd like the error response to be in JSON:API format as well, so we set that format here, in a higher priority (earlier running) filter. This works so long as the resource format can be determined before running any other filters, which is the case for JSON:API resources per above.

Methods

RouteCollection
filter(RouteCollection $collection, Request $request)

Filters the route collection against a request and returns all matching routes.

static string
getDefaultFormat(RouteCollection $collection)

Determines the default request format.

static string[]
getAvailableFormats(RouteCollection $collection)

Gets the set of formats across all routes in the collection.

Details

RouteCollection filter(RouteCollection $collection, Request $request)

Filters the route collection against a request and returns all matching routes.

Parameters

RouteCollection $collection

The collection against which to match.

Request $request

A Request object against which to match.

Return Value

RouteCollection

A non-empty RouteCollection of matched routes

Exceptions

ResourceNotFoundException

static protected string getDefaultFormat(RouteCollection $collection)

Determines the default request format.

By default, use 'html' as the default format. But when there's only a single route match, and that route specifies a '_format' requirement listing a single format, then use that as the default format. Also, if there are multiple routes which all require the same single format then use it.

Parameters

RouteCollection $collection

The route collection to filter.

Return Value

string

The default format.

static protected string[] getAvailableFormats(RouteCollection $collection)

Gets the set of formats across all routes in the collection.

Parameters

RouteCollection $collection

The route collection to filter.

Return Value

string[]

All available formats.