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
- <?php
- class RestComponent extends Object {
- }
- ?>
- <?php
- class RestHelper extends Helper
- {
- }
- ?>
in views/layouts make a folder called ‘rest’ and put a default.thtml file in it
- <?php echo $content_for_layout; ?>
- <?php e(‘<?xml version="1.0" encoding="utf-8" ?>‘);
- if (isset($applications) and !empty($applications)) : ?>
- <rsp stat="ok">
- <applications type=’array‘>
- <?php foreach ($applications as $application) : ?>
- <application type=’struct‘>
- <name><?php e($application['Application']['name'])?></name>
- <version><?php e($application['Version'][0]['value'])?></version>
- </application>
- <?php endforeach; ?>
- </applications>
- <?php else: ?>
- <rsp stat="fail">
- <err type=’struct‘>
- <?php if ($session->check(‘Message.flash’)): ?>
- <msg><?php e(strip_tags($session->read(‘Message.flash’)));?></msg>
- <?php endif; ?>
- </err>
- <?php endif; ?>
In the controller you can add logic like below to control output paths based on the service.
- if ($this->params[‘webservices’] == ‘Rest’) {
- $this->set(‘order’,$this->Order->read());
- }
- else {
- $this->redirect(‘/orders/index’);
- }
I’ll work on fleshing this out later and then finish the article I started in the bakery, I promise.
You’re currently reading “Quick and Dirty getting your CakePHP to REST”, an entry on Sam's random musings
- Published:
- 11.29.06 / 9am
- Category:
- Uncategorized
- Tags:
No comments
Jump to comment form | Comments RSS [?] | Trackback URI [?]