In a previous post I talked about how to use the built-in web services in CakePHP 1.1.x.x. A very popular piece too according to my site statistics too. Well, I’ve discovered that it’s even easier in Cake 1.2.x.x. I bugged Nate via IM for some tips on how to put it together, and it’s really very simple.
The biggest change is that the old way of doing custom routing in web services is gone in favour of handling them via extensions. In the past, you would go into app/config/core.php and enable the special web services routing. I was trying to do a web service with the URL http://localhost/cake_dev/foo and have it spit out an XML response. According to what I was told “…going forward the routes part / extensions on the end of the URL is the recommended way of doing it”. So, if I was doing this the standard way I would be calling http://localhost/cake_dev/foo/index.xml. I just like the cleaner URL. Now, if I was to do it the proper way, I would add in the following to my app/config/routes.php file:
* Add in support for web services by enabling generating output based on extension
Now, go and create your controller that will handle spitting out some XML:
* Test controller for built-in web services in Cake 1.2.x.x
* @author Chris Hartjes
class FooController extends AppController
var $components = array('RequestHandler');
var $uses = '';
var $helpers = array('Text', 'Xml');
$message = 'Testing';
$this->viewPath .= '/xml';
$this->layoutPath = 'xml';
<bar>< ?php print $message; ?></bar>
<?xml version="1.0" encoding="UTF-8" ?>
I’m hoping to use this setup for some web services I have to write for work but my boss likes some other framework, which isn’t surprising since he used to work for them. I will continue to fight for my right to eat Cake. Besides, it’s all part of my evil plan to get paid to contribute code to the CakePHP project. I mean, if I need to create an interactive console to do testing then I can build it with Cake and give it back to the community while getting paid at the same time. What could be better?