我将停用一个用作 API 的自定义 PHP 项目。
我需要支持这样的 URL,例如
/post/{id}/copy.json(复制带有 {id} 的帖子 /post(所有帖子)/post/{id}(返回一个帖子)
到目前为止一切正常,但是我发现路由很困难。我希望 Zend 检测它是什么类型的请求,然后使用正确的方法。我倾向于将其放入 My_Controller_Rest 预调度中,将变量传递回控制器,然后在 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