API として使用されていたカスタム PHP プロジェクトを廃止します。
次のような URL をサポートする必要があります。
/post/{id}/copy.json ({id} の投稿をコピー /post (すべての投稿) /post/{id} (1 つの投稿を返す)
これまでのところ順調ですが、ルーティングが難しいと感じています。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