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

PHP

This is Kusakabe from the web team.

I will write about specifying the output format using the Rest controller of FuelPHP (1.7)

There are several ways to specify this, so let's go through them in order!

 

By default, check the contents of Accept

The default Rest configuration is written in fuel/core/config/rest.php, and the output format is

'ignore_http_accept' => false, 'default_format' => 'xml',

This means that

  • Do not ignore the Accept request header
  • If no format is specified, output will be in XML format

That's it

Now, if you try sending a request using the Advanced REST client without changing the Accept header,

    Accept: */*

It will be interpreted as "Any format is OK" → "Then please use the default XML."

Here, in the Headers section of the Advanced REST client,

    Accept: */json

If you write this and make a request, you will get "JSON" followed by "Okay, JSON please."

 

Ignore the Accept content

Let's try overriding the configuration to ignore the contents of the Accept request header

Create fuel/app/config/rest.php and

 return array( 'ignore_http_accept' => true, 'default_format' => 'xml', );

In this state, even if you specify "Accept: */json", it will be ignored and the message will change to "JSON" → "OK, XML please."

 

Specifying by URI

If you add a format like .xml to the end of the action name in the URI, it will be returned in that format regardless of the configuration

    http://localhost/test/index.xml

 

Specification by controller properties

The format property of the controller takes precedence over anything else

    protected $format = null;

By default, null is set, but if you specify a format here, it will be returned in that format without question

 

priority

In summary, the priority is

Specifying by Accept < Specifying by URI < Specifying by property

It is as follows

 

Or rather

It's all explained in the documentation

If you found this article helpful , please give it a like!
0
Loading...
0 votes, average: 0.00 / 10
1,586
X facebook Hatena Bookmark pocket

The person who wrote this article

About the author