Quick and Dirty getting your CakePHP to REST

For CakePHP 1.1xxx

Here are the things I did to get it to work at a bare minimum for a
REST style service

Turn on webservices in config/core.php by uncommenting this line (or changing ‘off’ to ‘on’)

`define(‘WEBSERVICES’, ‘on’);`

make a rest.php file in controllers/components

  1. <?php
  2. class RestComponent extends Object {
  3. }
  4. ?>

make a rest.php file in views/helpers

  1. <?php
  2.    class RestHelper extends Helper
  3.    {
  4.    }
  5. ?>

in views/layouts make a folder called ‘rest’ and put a default.thtml file in it

  1. <?php echo $content_for_layout; ?>

for the controller that you want a rest service make a folder called
rest in views/controllername/
in there put the view for the action like

  1. <?php e(‘<?xml version="1.0" encoding="utf-8" ?>‘);
  2. if (isset($applications) and !empty($applications)) :  ?>
  3. <rsp stat="ok">
  4. <applications type=’array‘>
  5.        <?php foreach ($applications as $application) : ?>
  6.                <application type=’struct‘>
  7.                        <name><?php e($application['Application']['name'])?></name>
  8.                        <version><?php e($application['Version'][0]['value'])?></version>
  9.                </application>
  10.        <?php endforeach; ?>
  11. </applications>
  12. <?php else: ?>
  13. <rsp stat="fail">
  14.        <err type=’struct‘>
  15.        <?php if ($session->check(‘Message.flash’)): ?>
  16.                <msg><?php e(strip_tags($session->read(‘Message.flash’)));?></msg>
  17.        <?php endif; ?>
  18.        </err>
  19. <?php endif; ?>

In the controller you can add logic like below to control output paths based on the service.

  1. if ($this->params[‘webservices’] == ‘Rest’) {
  2.         $this->set(‘order’,$this->Order->read());
  3. }
  4. else {
  5.         $this->redirect(‘/orders/index’);
  6. }

I’ll work on fleshing this out later and then finish the article I started in the bakery, I promise.

This entry was posted in Uncategorized by Sam. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>