No REST for the wicked
I'm amazed that the Zend Framework team considered their REST server as "mission accomplished" when it does not, out of the box, handle GET/POST/PUT/DELETE. In order to get these systems to operate, you have to extend and augment the server class.
There is a controller based version of this, but again, that kind of undermines the purpose of the server classes - to provide a lightweight alternative to the MVC system for services.
An argument can be made that the parent class' functionality is kind of moot at this point - really, a variation of this class as a wholly self contained server would probably be just as efficient.
Step 1: extend the Zend Rest Server Class
To interpret request modes into methods, you have to extend the Zend_Rest_Server class.
The base url in the constructor is the path up to the part of the url that contains the ID. it does not necessarily have to have the domain, or to start from the root.
<?php
class Wll_Rest_Server
extends Zend_Rest_Server {
private $_base_url;
public function __construct($base) {
$this->_base_url = $base;
parent::__construct();
}
public function handle($request = false) {
$http = new Zend_Controller_Request_Http();
$id = preg_replace("~^.*{$this->_base_url}~", '', $_SERVER['REQUEST_URI']);
$id = trim($id, ' /');
if ($http->isPut()) {
$request = array(
'method' => 'put',
'id' => $id,
'value' => $http->getRawBody(),
'base' => $this->_base_url
);
} elseif ($http->isDelete()) {
$request = array(
'method' => 'delete',
'id' => $id,
'base' => $this->_base_url
);
} elseif ($http->isGet ()) {
$request = array(
'method' => 'get',
'id' => $id,
'base' => $this->_base_url
);
} elseif ($http->isPost()){
$request = array(
'method' => 'post',
'value' => $http->getRawBody(),
'base' => $this->_base_url
);
}
error_log(__METHOD__ . ': request = ' . print_r($request, 1));
parent::handle($request);
}
}
The other half to this is that the implementing class has to implement the Rest_Handler_IF. Note its variables line up with the request signatures above.
/**
*
* @author bingomanatee
*/
interface Wll_Rest_Handler_IF {
/**
* @var $id scalar
* @var $base string
*/
function get($id, $base = '');
/**
* @var $id scalar
* @var $value string
* @var $base string
*/
function put($id, $value, $base);
/**
* @var $value string
* @var $base string
*/
function post($value, $base);
/**
* @var $id scalar
* @var $base string
*/
function delete($id = null, $base = '');
}
You'll also need the .htaccess clipper in the service directory. (presuming your service is in index.php.) I ripped off the Zend Framework one, but you can get more specific if you like.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
example pending

Post new comment