Content negotiation and API mode¶
MIME types¶
MVC::Keayl::Mime is the format registry. mime-type maps a format to its MIME
type and mime-format maps a type (ignoring parameters and aliases) back to a
format. register-mime adds or extends entries:
1 2 3 | |
parse-accept orders an Accept header's types by quality, and negotiate
chooses the best of a list of available formats, honouring */* and type/*
wildcards and falling back to the first available format when there is no
preference:
1 | |
respond-to¶
A controller action serves several formats with respond-to, taking an ordered
list of format-to-block pairs. The format is taken from the path extension if
present, otherwise negotiated from the Accept header; the first declared format
is the default. A request that matches none gets 406:
1 2 3 4 5 6 | |
/posts.json forces JSON; /posts with Accept: text/html serves HTML.
Variants¶
A variant selects a device-specific view of the same format. request.variant
holds the variant, set explicitly or from a user-agent heuristic:
1 2 | |
A format block in respond-to may be a map of variant names to blocks instead of
a single block. The block for the current variant runs, falling back to any:
1 2 3 4 5 6 | |
Variants also steer template lookup. When the variant is set, the renderer
prefers a variant template before the plain one, so phone resolves
show.html+phone.haml and falls back to show.html.haml.
API controllers¶
MVC::Keayl::APIController is a JSON-first controller. It needs no view renderer:
rendering a bare object or a collection produces JSON, while string templates and
explicit render options still work as on a normal controller.
1 2 3 4 5 | |
A model is serialized through serialize, which calls the object's to-hash
(recursing into collections) by default. Provide a serializer to control the
shape:
1 | |