Alfredo Quiroga-Villamil wrote:
> This should be simple, yet for some reason can't find how to do it.
>
> I need to render XML.
>
> What is the correct way to accomplish this?
I use XML in my Cake application and here is what I've done:
In my routes.php, I added the line:
Router::parseExtensions('xml');
and included the "RequestHandler" component in my appropriate
controller(s).
Doing that allowed me to have simple urls like "/posts/view/10.xml" or
even "/posts.xml". When this extension is used, CakePHP sets a flag
indicating XML is being requested. You then can do something like this
in your controller action(s):
function view($id = null) {
if ($this->RequestHandler->isXml())
$post = $this->Post->getForXML($id));
else
$post = $this->Post->getNormalViewData();
$this->set(compact('post'));
}
Or you can just use the same query as you use for normal views. For my
application, my XML views do not need anywhere near the amount of
information I need for my normal views. Therefore, I have a separate
function that utilizes a query that only pulls in what's absolutely
needed for XML output.
Then all you need to do is create an "xml" subdirectory within your
controller view directory and create the necessary action named ctp
files for your XML output. For the view action example, the "blog/
views/posts/xml/view.ctp" file might be as simple as this (if the
controller is set up to use the XML helper):
<post>
<?php $xml->serialize($post); ?>
</post>
or you can output the format of the XML your own way if serialize()
doesn't do what you want.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---