«
»

PHP

Using CakePHP’s Native Web Service Support

11.29.06 | 13 Comments

Recently there was a posting on the CakePHP mailing list from someone complaining that CakePHP's native web service support didn't work, mainly because he couldn't figure out how to get it to work. So, I took up the challenge and sent out an email to the list showing that you can indeed use them. Might I add that until I tried this, I had never built a web service with CakePHP before. I found that by turning on the debug mode and actually following the instructions that were in the error messages (best undocumented feature of CakePHP if you ask me) I was able to build it. Oh yeah, a big shout-out to Samuel DeVore for his near-simultaneous reply to the original message showing an ever simpler example than mine. Which I promptly stole and used it in this example. :)

This example works with the latest stable version of CakePHP (1.1.10.3825) running on a Gentoo Linux box with PHP 5.1.2 and Apache 2. It's my understanding that the native web service stuff was originally built for Cake 1.2 (not released yet) and back-ported to Cake 1.1. Perhaps some key parts are missing, hence the need for some hackery mentioned below.

First, open up /app/config/core.php and turn on support for internal web services:

PHP:
  1. /**
  2. *  The define below is used to turn cake built webservices
  3. *  on or off. Default setting is off.
  4. */
  5.     define('WEBSERVICES', 'on');

Now, you have to understand that CakePHP won't magically spit out your content as XML, or SOAP, or as REST or whatever. The internal web services support provides built-in URLs for spitting out your content in whatever alternate format you want. This concept will become clear as we continue on here. Basically it lets you do the following:

  • mydomain/controller/action for regular content
  • mydomain/xml/controller/action to spit out your content formatted as XML

Okay, so first thing I did was create a test controller:

PHP:
  1. ?php
  2.  
  3. /**
  4. * Test controller for built-in web services
  5. *
  6. * @author Chris Hartjes
  7. *
  8. */
  9.  
  10. class TestController extends AppController
  11. {
  12.     var $name = 'Test';
  13.     var $uses = "";
  14.     var $components = array('xml');
  15.    
  16.     function index()
  17.     {
  18.         $message = "This is a test of built-in web services";
  19.         $this->set('message', $message);
  20.     }
  21. }
  22.  
  23. ?>

Those of you who are observant about CakePHP will notice that I've dragged in a component to be used. Currently, you need to create a component *and* a helper to make the web service stuff work. This is hackish, and hopefully goes away.

You need to create an XML component and put it in /app/controllers/components/xml.php

PHP:
  1. <?php
  2.  
  3. class XMLComponent extends Object
  4. {
  5.     // Blank for now as we're not doing anything weird
  6. }
  7.  
  8. ?>

and then create an XML helper, placing that in /app/views/helpers/xml.php

PHP:
  1. <?php
  2.  
  3. class XmlHelper extends Helper
  4. {
  5.     // blank for now
  6. }
  7.  
  8. ?>

Now we need to generate a bunch of thtml files to display our output. Of course, we have to create the default view for our action in the controller. Put this in /app/views/test/index.thtml

PHP:
  1. Content generated via regular view<br />
  2. <?php echo $message ?>

Next is the XML stuff. There are two files we need to create. One is /app/views/layouts/xml/default.thtml, which simply tells CakePHP to use whatever data is passed into it.

PHP:
  1. <?php echo $content_for_layout ?>

and then we create /app/views/test/xml/index.thtml to spit out our data in XML

PHP:
  1. <?php e('<?xml version="1.0" encoding="utf-8" ?>' . "\n"); ?>
  2. <foo>
  3. <bar><?php echo $message ?></bar>
  4. </foo>

I believe the e(blah) function is a built-in helper for CakePHP.

So, now when we go to mydomain/test we get the following output generated:

HTML:
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <title>CakePHP Web Services Test</title>
  4. <link rel="stylesheet" type="text/css" href="/css/cake.generic.css" /><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /></head>
  5.     <div id="container">
  6.         <div id="content">
  7.             Content generated via regular view<br />
  8. This is a test of built-in web services  </div>
  9.  
  10.         <div id="footer">
  11.             &nbsp;
  12.         </div>
  13.     </div>
  14.     </body>
  15. </html>

and when we go to mydomain/xml/test we get the following output generated

XML:
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <foo>
  3. <bar>This is a test of built-in web services</bar>
  4. </foo>

Like I said before, a lot of hoops to jump through and hopefully those go away when CakePHP 1.2 comes out.

13 Comments


«
»