cakePHP + extjs行编辑器和REST

时间:2010-09-15 15:03:40

标签: rest cakephp extjs

我已经在cakePHP中实现了REST路由,以便将REST样式请求正确地路由到我控制器中的正确方法。

这是我添加到我的routes.php

的内容
Router::mapResources(array('object_fields'));

这正确地将REST请求路由到我的控制器内的索引/添加/编辑/删除方法。

在我的EXTJS网格中,我使用行编辑器和一个restful商店来实现CRUD行为。

这是我网格的代码

myapp.object_field_grid = Ext.extend(Ext.grid.GridPanel, {
  closable: true,
    stripeRows: true,

    frame: true,
    viewConfig: {
        forceFit: true
    },
    editor: new Ext.ux.grid.RowEditor({
     saveText: 'Update',

     }),

    onAdd : function(btn, ev){
     var u = new this.store.recordType({
      name : '',
      type: '',
     });

     this.editor.stopEditing();
             this.store.insert(0, u);
             this.editor.startEditing(0);


  },

  onDelete : function(){
  },

  initComponent: function() {

   var proxy = new Ext.data.HttpProxy({
   url: 'object_fields/',


   });

   var reader = new Ext.data.JsonReader({

   totalProperty: 'totalCount',
   successProperty: 'success',
   idProperty: 'id',
   root: 'data',
   messageProperty: 'message'  
   }, [
       {name: 'id'},
        {name: 'name', allowBlank: false},
       {name: 'type', allowBlank: false},
   ]);


   var writer = new Ext.data.JsonWriter({
      encode: false,

   });

   var store = new Ext.data.Store({
    baseParams: {id: this.object_id},
    id: 'object_fields',
       restful: true,     
       proxy: proxy,
       reader: reader,
         writer: writer,
       });

   store.load();

   var object_field_columns =  [
     //  {header: "id", width: 250, sortable: true, dataIndex: 'id', editor: new      Ext.form.TextField({})},                         
       {header: "name", width: 250, sortable: true, dataIndex: 'name', editor: new     Ext.form.TextField({})},
      {header: "type", width: 250, sortable: true, dataIndex: 'type', editor: new    Ext.form.ComboBox({editable: false, store:['STRING', 'NUMBER']})},
   ];


  var config = {
    columns: object_field_columns,
          store: store,
          plugins: [this.editor],
          //autoHeight: true,
          height: 200,
          tbar: [{
              text: 'Add',
              iconCls: 'silk-add',
              handler: this.onAdd,
              scope: this,
          }, '-', {
              text: 'Delete',
              iconCls: 'silk-delete',
              handler: this.onDelete,
              scope: this,
          }, '-'],
  }





   Ext.apply(this, Ext.apply(this.initialConfig, config));


   myapp.object_field_grid.superclass.initComponent.apply(this, arguments);


  },
  onRender: function() {
        this.store.load();
        myapp.object_field_grid.superclass.onRender.apply(this, arguments);
    }
});

Ext.reg('object_field_grid', myapp.object_field_grid); // register xtype

我的GET / POST请求被正确地路由到我的控制器内的索引/添加方法,我能够轻松检索我在请求中传递它的参数。

我的问题是更新功能PUT请求。 PUT请求确实成功路由到控制器内的编辑方法。

这就是firebug中的请求

http://server.local/object_fields/20
JSON



data
Object { name="test7777777777", more...}


id
"18"
Source
{"id":"18","data":{"name":"test7777777777","id":"20"}}

在我的编辑方法中,我没有收到我通过PUT请求传递的数组。 当我在我的编辑方法中转储$ this-> params时,这就是数组中的内容。

([id] => 20
[named] => Array
    (
    )

[pass] => Array
    (
        [0] => 20
    )

[controller] => object_fields
[action] => edit
[[method]] => PUT
[plugin] => 
[url] => Array
    (
        [ext] => html
        [url] => object_fields/20
    )

[form] => Array
    (
    )

[isAjax] => 1
)

如何在编辑方法中通过PUT请求正确接收我的数组?

更新:

我可以使用编辑方法

中的以下代码检索我的数组
function edit($id){
    $this->autoRender = false;
    echo 'edit';

     $raw  = '';
        $httpContent = fopen('php://input', 'r');
        while ($kb = fread($httpContent, 1024)) {
            $raw .= $kb;
        }
        fclose($httpContent);
        $params = array();
        parse_str($raw, $params);

    print_r($params);

}

现在的问题是为什么cakePHP不会自动执行此操作?

1 个答案:

答案 0 :(得分:0)

将它放在你的app_controller.php中:

public function beforeFilter() {

    if (
        isset($this->RequestHandler) &&
        $this->RequestHandler->requestedWith('json') &&
        (
            $this->RequestHandler->isPost() ||
            $this->RequestHandler->isPut()
        )
    ) {

        $jsonData = json_decode(utf8_encode(trim(file_get_contents('php://input'))), true);

        if (is_array($jsonData)) {

            $this->data = $jsonData;
            unset($jsonData);

        }

    }


}