Question

How can I export data from a CakePHP 2.4 app as KML, not just XML and JSON?

This is what I've been trying:

1: I created a simple controller action, download, to find all data in my model:

$this->set('posts', $this->Post->find('all'));

2: I addeded the file type in routes.php:

Router::parseExtensions('json', 'xml', 'kml', 'csv');

3: I created a folder for my data view file type:

/app/View/Post/kml

4: I created a view, download.ctp, which manually echos out the XML tags for the KML file.

Now comes the problem: When I place this view file under /app/View/Post/xml, visiting /posts/download.xml creates a valid KML document I can open in Google Earth, albeit with an .XML extension. When I place the file under /app/View/Post/kml, and visit /posts/download.kml, it throws an error: View file "/home/public/app/View/Themed/Cakestrap/Posts/download.ctp" is missing.

I tried copying my view to the above location, but that just echos the XML into my standard layout. What's the proper way to create data views for filetypes other than XML and JSON?

Was it helpful?

Solution

Saddly KML does not have a mime type recognized in servers vide Google. So when you add it to Router::ParseExtensions(...) Cake can't see that extension and won't try to access your download.ctp inside kml folder.

You have two alternatives to setup this to work:

  • Change server settings in order to it recognize the extension Google Doc.
  • Try to call render manually like this:

    <?php
    $this->response->type("application/vnd.google-earth.kml+xml");
    $this->render('kml/download.ctp', 'xml');
    
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top