ContentType attribute
1Presentation
The \Temma\Attributes\ContentType attribute defines the content type of the response, for a controller or an action. The view sends it in the HTTP Content-Type header, instead of its default content type.
It is equivalent to the _contentType() method on controllers, in the same way as the Template attribute is equivalent to the _template() method and the View attribute to the _view() method.
Typical use cases: serving a robots.txt file or a Markdown document generated by a Smarty template, or adapting the content type to a format that is only known at runtime.
2Parameter
The attribute offers a single, optional parameter:
-
$contentType: (null|string) Content type of the response. It can be a full MIME type (text/markdown, image/png, text/plain; charset=ISO-8859-1) or an alias (json, csv…).
Equivalent to the _contentType() method on controllers.
This parameter can be omitted or explicitly set to null, in which case the content type is reset to its default value, i.e. the one of the view in use. This is useful on an action when the attribute is set on the controller.
An unknown alias throws a \Temma\Exceptions\Framework exception (code \Temma\Exceptions\Framework::CONFIG).
The attribute can be applied to a controller (it then applies to all its actions) or to an action. It is not repeatable. When several definitions coexist, the most specific one wins:
- a call to the _contentType() method in the action's code;
- the attribute set on the action;
- the attribute set on the controller.
3Aliases
Any value that does not contain a "/" character is considered an alias, looked up case-insensitively in the following table. These are the same aliases as those used for the content negotiation of the View attribute; they are defined in the \Temma\Web\View::MIME_ALIASES constant.
| Alias | MIME type |
|---|---|
| html | text/html |
| xhtml | application/xhtml+xml |
| json | application/json |
| rss | application/rss+xml |
| csv | text/csv |
| calendar | text/calendar |
4Character set
If the content type is of the form text/* (or is application/rss+xml or application/xhtml+xml) and does not already contain a character set indication (charset), the view appends ; charset=UTF-8 to it when sending. In all other cases, the value is sent as is.
- text/plain is sent as Content-Type: text/plain; charset=UTF-8;
- text/plain; charset=ISO-8859-1 is sent unchanged;
- rss (alias of application/rss+xml) is sent as Content-Type: application/rss+xml; charset=UTF-8;
- image/png or application/json are sent unchanged.
5Examples
To make all the actions of a controller return Markdown:
use \Temma\Attributes\ContentType as TµContentType;
#[TµContentType('text/markdown')]
class Doc extends \Temma\Web\Controller {
// ...
}
To make a single action return Markdown:
use \Temma\Attributes\ContentType as TµContentType;
class Doc extends \Temma\Web\Controller {
#[TµContentType('text/markdown')]
public function page() {
// ...
}
}
To use an alias, and to return to the view's default type on an action while the attribute is set on the controller:
use \Temma\Attributes\ContentType as TµContentType;
// all actions return Markdown
#[TµContentType('text/markdown')]
class Doc extends \Temma\Web\Controller {
// sent as "text/markdown; charset=UTF-8"
public function page() {
// ...
}
// this action returns to the view's default type
// (here "text/html; charset=UTF-8" with the Smarty view)
#[TµContentType]
public function html() {
// ...
}
// alias: sent as "application/json"
#[TµContentType('json')]
public function data() {
// ...
}
}
The attribute combines naturally with the Smarty view: the template generates content that is not HTML, and the attribute defines the matching content type. For example, to serve a robots.txt file generated from the templates/site/robots.tpl template, and a Markdown document generated from the templates/site/readme.tpl template:
use \Temma\Attributes\ContentType as TµContentType;
class Site extends \Temma\Web\Controller {
// sent as "text/plain; charset=UTF-8"
#[TµContentType('text/plain')]
public function robots() {
$this['sitemap'] = 'https://www.example.com/sitemap.xml';
}
// sent as "text/markdown; charset=UTF-8"
#[TµContentType('text/markdown')]
public function readme() {
$this['title'] = 'Documentation';
// ...
}
}
It is of course possible to combine the attribute and the _contentType() method, for example when the content type depends on the request. The call in the action takes precedence over the attribute:
use \Temma\Attributes\ContentType as TµContentType;
// by default, pages are sent as Markdown
#[TµContentType('text/markdown')]
class Doc extends \Temma\Web\Controller {
// URL: /doc/page/install or /doc/page/install/txt
public function page(string $name, ?string $format=null) {
if ($format == 'txt') {
// send as plain text
$this->_contentType('text/plain');
}
// ...
}
}