Hi Kabel,
Has anyone else done this or have any recommendations? Is it a
good idea to
try to run a Zend_Rest_Server from inside a controller, or should I
stick
with a simple one-off script?
How about this example (complete with rudimentary logging):
class RestController extends Zend_Controller_Action
{
protected $_server;
protected $_application_directory;
public function init()
{
$this->_server = new Zend_Rest_Server();
$this->_helper->viewRenderer->setNoRender();
$this->_application_directory = Zend_Registry::get
('application_directory');
}
public function indexAction()
{
$writer = new Zend_Log_Writer_Stream($this-
>_application_directory . 'rest.log');
$logger = new Zend_Log($writer);
$headers = apache_request_headers();
$headerString = '---------------------------' . "\n\n";
foreach ($headers as $header => $value) {
$headerString .= "$header: $value\n";
}
$logger->info($headerString);
require_once 'MyApp/Service/Wotsit.php';
$this->_server->setClass('MyApp_Service_Wotsit');
$this->_server->handle();
}
public function clientAction()
{
$wotsit = new MyApp_Service_Wotsit;
$server = 'http://www.yoursite.com/rest/';
$client = new Zend_Rest_Client($server);
$response = $client->doSomething()->get();
if ($response->isSuccess()) {
var_dump($response);
} else {
echo $wotsit->doSomething();
}
}
}
...Nick