For those times when you need a quick and dirty webservice for your data.
While working on a freelance app, I realized I was spending way too much time trying to create a well-maintained separation between the API and the frontend. There was no need to do this, the api users would figure out how to use it in time. So I decided to create a View class to auto-transform the data into JSON and XML. The result is the Webservice View
.
Later, I wanted to move an entire controller to webservice, but have it also optionally render html. As a result, I created the Webservice Component
.
- CakePHP 1.3 (untested in 1.2)
- PHP5.2 for native json_decode
[Manual]
- Download this: http://github.com/josegonzalez/webservice_plugin/zipball/master
- Unzip that download.
- Copy the resulting folder to app/plugins
- Rename the folder you just copied to
webservice
[GIT Submodule]
In your app directory type:
git submodule add git://github.com/josegonzalez/webservice_plugin.git plugins/webservice
git submodule init
git submodule update
[GIT Clone]
In your plugin directory type
git clone git://github.com/josegonzalez/webservice_plugin.git webservice
Specify the extensions you’d like to parse in config/routes.php, for example:
Router::parseExtensions('json');
Attach the RequestHandler Component
and the Webservice Component
to your controller for an instant automagic webservice:
<?php
class PostsController extends AppController {
var $components = array('RequestHandler', 'Webservice.Webservice');
}
?>
Or simply set the Webservice View
class where necessary (don’t forget parseExtensions() and setContent()):
<?php
class PostsController extends AppController {
function index() {
$this->view = 'Webservice.Webservice';
$posts = $this->paginate();
$this->set(compact('posts'));
}
}
?>
Views are not necessary, the View class takes care of everything.
1. Unit Tests
2. More thorough documentation
3. YAML and Serialized PHP output
Copyright © 2010-2011 Jose Diaz-Gonzalez
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the “Software”), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.