我正在退休一个用作API的自定义PHP项目。
我需要支持此类网址,例如
/ post / {id} /copy.json({id}复制帖子 / post(所有帖子) / post / {id}(返回一个帖子)
到目前为止一切顺利,但是路由我发现很难。我希望Zend检测它是什么类型的请求,然后使用正确的方法。我倾向于将它放在My_Controller_Rest predispatch中,将变量传递回控制器,然后在indexAction中执行$ this-> getAction()
思考?我有点假设使用Zend_Rest_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()
{
}
}
我的控制器休息
<?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;
}
}
更新:
我通过使用Resauce:https://github.com/mikekelly/Resauce
解决了这个问题您可以在此处查看我的改进:https://github.com/mikekelly/Resauce/issues/1