I am retiring a custom PHP project that was used as an API.
I need to support such URLS such as
/post/{id}/copy.json (copy post with {id} /post (all posts) /post/{id} (return one post)
So far its going okay, however routing I'm finding difficult. I would like Zend to detect what type of request it is and then use the correct method. I am leaning towards putting that in My_Controller_Rest predispatch, passing a variable back to the controller and then in indexAction doing $this->getAction()
Thoughts? I kind of assumed using Zend_Rest_Controller it would fire off the right method, and unless I'm doing it wrong, its not.
All my controllers extend an abstract class,
Controller:
<?php
class Post_IndexController extends My_Controller_Rest
{
public function init()
{
}
public function indexAction()
{
}
public function getAction()
{
}
public function postAction()
{
}
public function putAction()
{
}
public function deleteAction()
{
}
}
My Controller Rest
<?php
abstract class My_Controller_Rest extends Zend_Rest_Controller {
public function preDispatch() {
$this->_helper->viewRenderer->setNoRender(true);
$request = Zend_Controller_Front::getInstance()->getRequest();
$this->output = null;
//get registry versions
$config = Zend_Registry::get('config');
$cache = Zend_Registry::get('cache');
$log = Zend_Registry::get('log');
$loggers = Zend_Registry::get('loggers');
//internal to the view
$this->_config = $config;
$this->_cache = $cache;
$this->_log = $log;
$this->data = array();
$this->data['module'] = $request->getModuleName();
$this->data['controller'] = $request->getControllerName();
$this->data['action'] = $request->getActionName();
$this->data['loggers'] = $loggers;
}
public function postDispatch() {
header('Content-type: text/json');
header('Content-type: application/json');
echo json_encode(array('output' => $this->output, 'data' => $this->data));
exit;
}
}
UPDATE:
I resolved this by using the Resauce: https://github.com/mikekelly/Resauce
You can find my improvements by looking here: https://github.com/mikekelly/Resauce/issues/1