Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Customising XML Basenode / Entity Name in REST Response
  • I'm mucking around with Fuel to get a feel as to whether it's appropriate for a new project. I've setup a basic REST controller:
    <?php
    class Controller_Artist extends Controller_Rest {
    
        public function get_list()
        {
            $results = Model_Artist::find( 'all', array(
                'related' => array( 'tracks' )
            ) );
         
            $this->response( $results );
        }
    }
    

    This produces the following result:
    <xml>
      <item>
        <id>1</id>
        <name>First Artist</name>
        <tracks>
          <track>
            <id>1</id>
            <name>First Track</name>
          </track>
          <track>
            <id>2</id>
            <name>Second Track</name>
          </track>
        </tracks>
      </item>
      <item>
        <id>2</id>
        <name>Second Artist</name>
        <tracks>
          <track>
            <id>1</id>
            <name>First Track</name>
          </track>
        </tracks>
      </item>
    </xml>
    

    How can I customise the first xml entity name to 'artists' instead of 'item'. Or even better produce a result such as this:
    <artists>
      <artist>
        <id>1</id>
        <name>First Artist</name>
        <tracks>
          <track>
            <id>1</id>
            <name>First Track</name>
          </track>
          <track>
            <id>2</id>
            <name>Second Track</name>
          </track>
        </tracks>
      </artist>
     </xml>
    

    The core format class function to_xml has an option to change the basenode from xml to something else, but I cannot see how to pass what I require from the controller to this formatter. Do I have to eschew the auto-formatted response and force my own hand crafted xml response? Thanks. PS The formatting options don't seem to work and posting XML into this forum is very painful so apologies if this post doesn't turn out right.
  • You misunderstood my intention. I'm not asking you to hack Fuel, I am asking you to try something out and if it works send in a pull request. This is a community contributed framework and if somebody needs a feature to happen they need to not be affraid to give it a go. There are plenty of guides around on how user documentation works. I am insanely busy with about 4 freelance projects taking up just about any free time I have. I have made a suggestion, please try it out.
  • You're right, I did misunderstand, I also didn't appreciate you were one of the main contributors. Thank you for taking the time to try and help. Modifying the request controller to call \Format has done the trick. I'll try and learn git to do a pull request.
  • So I am currently trying another way to get around this, I am trying to extend the Format class to provide my own formatters. I've followed the documentation on extending core classes to create the following: In app/classes/format.php I have:
    class Format extends Fuel\Core\Format &#123; ... }
    

    And in app/bootstrap.php I have:
    Autoloader::add_classes(array(
     'Format' => APPPATH.'classes/format.php',
    ));
    

    However the Controller_Rest class has
    namespace Fuel\Core
    
    set so it always calls the Core Format class and not mine. Any ideas how to make the Controller call my class and not the core?
  • Kenzor: Try modifying the Rest controller to call \Format instead of Format. That should tell it to look in the app namespace, which will fall back to the core if nothing is there. As for customizing the basenode... yeah it's tough. We could have something awesome that detects if its a model and sets the basenode as a plural version of that, but its a bit of a crazy convention. If the basenode is <chickens> then <item> will automatically become <chicken> which is great, but we just need to work out a nice way to set it. OR use JSON ;-)
  • Phil Sturgeon wrote on Thursday 15th of September 2011:
    Kenzor: Try modifying the Rest controller to call \Format instead of Format. That should tell it to look in the app namespace, which will fall back to the core if nothing is there.

    Thanks for the tip. My understanding of frameworks though is that you shouldn't have to modify the core, and secondly if you do modify the core you are then preventing yourself from being able to upgrade it. Doesn't seem like the proper solution somehow. I tried to extend the Rest controller but again the documentation is so lacking I can't figure out how to do that either.

Howdy, Stranger!

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

In this Discussion