5.4. Alternate Formats

Perhaps you would like to display a list of your recent blog posts as xml for an RSS reader. Or perhaps you have tabular data that you would like to download as CSV data. Solar makes this easy using alternate formats.

First, you need to map the alternate formats to the action methods.

Open SYSTEM/source/acme/Acme/App/Blog.php and add the following $_action_format array to the _setup() method so it looks like the code below.

<?php
protected function _setup()
{
    parent::_setup();
    $this->_model = Solar_Registry::get('model_catalog');
    
    // allow xml as an action format when browsing the index action
    // browsing to index.xml will output to index.xml.php    
    $this->_action_format = array(
        'index' => array('xml')
    );
}   
?>

This tells Solar that if a user browses to http://localhost/blog/index.xml, then

  1. it should look for a view named index.xml.php,

  2. it should set the appropriate content type (application/xml), and

  3. it should turn off the layout.

Try this by creating a new view script called index.xml.php in the SYSTEM/source/acme/Acme/App/Blog/View folder.

Add the following text to the newly created view script.

<items>
<?php foreach ($this->list as $item) : ?>
<item>
    <title><?php echo $this->escape($item->title); ?></title>
</item>
<?php endforeach; ?>
</items>

Browse to http://localhost/blog/index.xml and you should see your page in xml format without a layout wrapping around it. Depending on your browser, you may have to view the source. No attempt was made to make this valid rss xml format.



Local