简体   繁体   中英

Default format for REST controller in FuelPHP

I've copied rest.php from core/config to app/config and modified default_format to json but if i don't append '.json' to the URL I still get response in XML format.

How can I get response in JSON on URLs without format?

To determine the format in which the result should be returned, the REST controller uses the following algorithm and selects the first that is found:

  1. use the protected property $format if it contains a supported format
  2. use the URL extension if it is a supported format
  3. use the format specified by the :format variable in the route if it contains a supported format
  4. use the format defined in the HTTP_ACCEPT header
  5. use the default value defined by the $rest_format property of your class

If you're performing an AJAX request you'll want to send up some accept headers up with the request. It's considered bad practice to hard code the response type server side.

In your controller,

protected $format = 'json';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM