Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
XML output on REST controller
  • I'm using a Rest Controller to generate XML response and want to change the
    default output tags. No problem changing "xml" to "other"

    public function post_index() {
      ...
      $this->xml_basenode = 'other';
      return $this->response(array(
        'baz' => array( 1, 50, 219 ),
      ));
      ...
    }

    gives, as suggested in the docs:

       <?xml version="1.0" encoding="utf-8"?>
       <other><baz><item>1</item><item>50</item><item>219</item></baz></other>

    My question is: How do I change "item" to, say "foo".

    I see where 'item' is set in fuel/core/classes/format.php:

       $key = (\Inflector::singularize($basenode) != $basenode) ?
              \Inflector::singularize($basenode) : 'item';

    But I cannot figure out how to set the "$basenode" used here to
    change the outcome to something other than 'item'.


    Thanks.

  • HarroHarro
    Accepted Answer
    The problem here is with singular and plural. The idea is that your collection is a plural word, and every item is the singular version of that. As an example:

    $ oil console
    Fuel 1.8-dev - PHP 5.6.15 (cli) (Oct 29 2015 14:58:50) [Linux]
    >>> $x = Format::forge(['cow' => [1,2]])
    >>> $x->to_xml()
    <?xml version="1.0" encoding="utf-8"?>\n<xml><cow><item>1</item><item>2</item></cow></xml>\n

    >>> $x = Format::forge(['cows' => [1,2]])
    >>> $x->to_xml()
    <?xml version="1.0" encoding="utf-8"?>\n<xml><cows><cow>1</cow><cow>2</cow></cows></xml>\n

    So you have a collection of cows, with cow 1 and cow 2.

    Since the singular of 'baz' is 'baz', and you can't have xml nodes with the same name, it uses "item" instead.
  • Thanks!

    I saw the singularize and didn't recognize the significance.

    Is there any way to do user modification of what is considered to be
    the "plural" of something?

    For example, I would like the singular of "patientlist" to be "patient" for
    one REST controller.

    Thanks.

  • HarroHarro
    Accepted Answer
    Inflector is driven by a language dependent ruleset. A default EN version can be found in fuel/core/lang/en/inflector.php.

    You can copy this to your app and add your own singular (plural -> singular) and plural (singular -> plural) rules to it. Rules are regex's.

    Avoid "uncountable_words", these can't be expressed as singular or plural. For example, there is no "one equipment", "two equipments", "equipment" is uncountable.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion