2010-09-15 37 views
0

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

这是我已经添加到我的routes.php文件

Router::mapResources(array('object_fields')); 

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

在我的EXTJS网格中,我使用了行编辑器和一个restful store来实现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请求被正确地路由到我的索引为我网该代码/加我的控制器内的方法和我能够很容易地检索,我通过它在PARAMATERS请求。

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

这是要求是什么样子的萤火

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请求正确接收我的数组?

UPDATE:

我可以使用编辑方法

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的没有做到这一点全自动内将下面的代码来获取我的阵列?

回答

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); 

     } 

    } 


} 
+0

请注意,你可能会遇到这个问题,PUT请求,但:http://cakephp.lighthouseapp.com/projects/42648/tickets/855-irrevocably -reading-phpinput功能于requesthelpercomponentstartup#票855-1 – deceze 2010-11-04 03:56:28