mysql dbに情報を保存するZend_Restを使用してRESTfulアプリケーションを実装しました。Backbone.js
でビューを処理します。
簡単なCRUDの例を探しています。どうやってするか?
Zend_Rest+Backbone を使用した例は見つかりませんでした。アイデアは、ここで一緒に作成することです。
アップデート 0.1
パラメータを (バックボーンから)送信し、(get/put/delete コントローラで)読み取るにはどうすればよいですか?
コントローラー (モジュール>api>コントローラー>BackboneController.php)
class Api_BackboneController extends Zend_Rest_Controller
{
public function init(){
$this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setNoRender(true);
}
public function headAction(){
$this->getResponse()->setBody(null);
}
public function optionsAction(){
$this->getResponse()->setBody(null);
$this->getResponse()->setHeader('Allow', 'OPTIONS, HEAD, INDEX, GET, POST, PUT, DELETE');
}
// called from backbone with "read"
public function indexAction(){
// get the params
$resp = json_decode(file_get_contents('php://input'));
// send the same response
$this->getResponse()->appendBody(json_encode($resp));
}
// I can't reach this one from backbone, WHY?
public function getAction(){}
// called from backbone with "update"
public function putAction(){}
// called from backbone with "delete"
public function deleteAction(){}
}
VIEW (モジュール>デフォルト>ビュー>スクリプト>index.phtml)
var MyModel = Backbone.Model.extend({
defaults: {
text: "default text"
},
url: "/base/api/backbone",
options: {
success: function(data){
console.log(data);
},
error: function(x, t, e){
console.log("error: " + t + ", " + e);
}
}
});
var myModel = new MyModel();
Backbone.sync("read", myModel, myModel.options);
})(jQuery);