[Osaka/Yokohama/Tokushima] Looking for infrastructure/server side engineers!

[Osaka/Yokohama/Tokushima] Looking for infrastructure/server side engineers!

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Successor to CentOS] AlmaLinux OS server construction/migration service

[Successor to CentOS] AlmaLinux OS server construction/migration service

[For WordPress only] Cloud server “Web Speed”

[For WordPress only] Cloud server “Web Speed”

[FuelPHP 1.7] Talk about the output format of the Rest controller

PHP

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

1
'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

1
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.

 

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.

If you found this article helpful , please give it a like!
0
Loading...
0 votes, average: 0.00 / 10
1,442
X facebook Hatena Bookmark pocket
[2026.6.30 Amazon Linux 2 end of support] Amazon Linux server migration solution

[2026.6.30 Amazon Linux 2 end of support] Amazon Linux server migration solution

The person who wrote this article

About the author