[FuelPHP 1.7] Talk about the output format of the Rest controller
table of contents
This is Kusakabe from the web team.
I will write a story about specifying the output format using the Rest controller of FuelPHP (1.7).
There are several ways to specify them, so follow them in order!
View the contents of Accept by default
The default settings for Rest are described in fuel/core/config/rest.php, and the output format is
'ignore_http_accept' => false, 'default_format' => 'xml',
It becomes. What this means is that
- Do not ignore the contents of Accept in the request header
- Output in XML format if no format is specified
That's what it means.
Now, in this state, if you use Advanced REST client and send a request without modifying the Accept header, you will get
Accept: */*
It is interpreted as ``Any format is OK'' → ``Then, please use the default XML.''
Here, in the Headers field of Advanced REST client,
Accept: */json
If you write and request it, it will say "JSON" → "Then JSON please."
Try ignoring the contents of Accept.
Let's try overwriting the configuration to ignore the Accept request header contents.
Create fuel/app/config/rest.php and
return array( 'ignore_http_accept' => true, 'default_format' => 'xml', );
It is written as In this state, even if you say "Accept: */json", it will be ignored and the message will be changed from "JSON" to "XML please."
Specification by URI
If you add a format like .xml to the end of the action name in the URI, that format will be returned regardless of the configuration.
http://localhost/test/index.xml
Specified by controller properties
The controller's format property takes precedence over everything else.
protected $format = null;
By default, it is set to null, but if you specify a format here, it will be returned in that format without any questions asked.
priority
In summary, the priority is
Specification by Accept < Specification by URI < Specification by property
It becomes.
Or rather
It's all written in the documentation Looks like things are going well later.