-1

I want to post data to Zend 2 restful controller from EXTJS 4 rest proxy. But when I inspect with the firebug I found that there is the post data ,but I am not able to fetch that data .How to get that post data to use it in my methods. Where I am getting wrong I cant figure it out. Anyone having steps to post data from rest Extjs 4 proxy method to Zend?

akond
  • 15,865
  • 4
  • 35
  • 55
anupkumar
  • 357
  • 4
  • 14
  • 28
  • How is your Z2 app expecting the data to be formatted, and how is ExtJS formatting it? It sounds like the ExtJS request is bring made okay, so my initial reaction is that your Z2 app is not configured to accept the request in the format that ExtJS is sending it. Care to share some snippet of the request and some bits from your Z2 code that shows how you're handing the inbound request? – existdissolve Jan 20 '13 at 12:26
  • share (at least) your server code – shampoo Jan 20 '13 at 18:25

3 Answers3

0

The standard way in PHP to load JSON from the HTTP Request Body is to use

$some_json = file_get_contents('php://input');
$some_object = json_decode($some_json);

I don't know whether Zend2 has their own way of doing it.

Neil McGuigan
  • 46,580
  • 12
  • 123
  • 152
0

ExtJS send params like sort, page, filter, etc. into the URL, e.g.: http://myhost.com/?page=2&sort=myfield.

In this case you simply need to intercept the query params from your RESTController Action:

$page   = $this->params()->fromQuery('page');
$sort   = $this->params()->fromQuery('sort');

Data transmission to a ZF2 REST controller depends on the HTTP Method you are using:

  • GET, PUT, DELETE receives an $id argument;
  • POST used to create a new record and receives a $data argument.

For further information you can have a look in here:

http://framework.zend.com/manual/current/en/modules/zend.mvc.controllers.html#the-abstractrestfulcontroller

If you are interested, I am developing my REST proxy for Sencha, I have tested with ExtJS and Sencha Touch frameworks and it supports pagination, remote filters, remote sorting and so on.

It's developed with Zend Framework 2 and may support different DB types on the same installation. Have a look at:

http://apiskeleton.asaconsult.com/

Armfoot
  • 4,663
  • 5
  • 45
  • 60
-1

I have exactly the same problem. Now, the reasons why the previous replies don´t work is because Zend Studio 10 has a new feature for configuring REST services from a visual editor that receives parameters and creates routes. This service designer is actually accepting the parameters form the body for the POST action of the Restful service but it somehow ignores the JSon sent from ExtJS. To make things worse, if you test the service using the integrated GUI in Zend Studio 10 it works perfectly and the JSon produced is 100% the same as the one from ExtJS ( I actually performed some data transforms to get to that point ), and it doesn't work.

The real issue is that there is 'something' within the POST method that is different in the ExtJS Request, than the JSon being expected from the framework itself that abstracts the code for deserializing this JSon from the developer.

The real question is, What is the Zend Server Gateway library expecting that ExtJS is sending in a different way in the POST body? I would enormously appreciate if any of the thousands of experts here could please check that out and come with a correct answer. The one that will answer this will have to have or to download a trial of Zend Studio 10 and check for himself how the Visual Service Tool that comes with ZS10 works. I have found so far that ExtJS4 produces it´s JSon like this:

{"data":
 {"IdCurso":"0","Nombre":"NodeJS","IdEstadoCurso":"1",
  "IdTipoPeriodo":"1","CantPeriodo":"1","CantAsignatura":"1","IdPlantel":"1"}
}

And the Zend GUI for testing the service, produces (and the service accepts) JSon like this:

{
    'data' : '{"Nombre":"NodeJS","IdEstadoCurso":"1","IdTipoPeriodo":"1","CantPeriodo":"1","CantAsignatura":"1","IdPlantel":"1","IdCurso":"0"}'
}

As you can see data is enclosed on single quotes, and its content is as well for Zend Framework to consume it without issue. ExtJS uses double quotes, as well as is not quoting the content of the data package itself.

This is a Patch that actually works, Not the definitive solution, if somebody finds a better one, that would be great.

Try this as the first instruction in your add() (POST) method:

$data = json_decode($this->getRequest()->getContent());

You may need to add afer that:

$data = (array) $data;

After that, keep writing your code with the $data variable normally as you would do if the parameter would actually work.

Mat
  • 202,337
  • 40
  • 393
  • 406
Will de la Vega
  • 536
  • 1
  • 5
  • 17
  • It is actually the very same question as my case is identical to this one. Actually I am providing more info and insight to the orginal question so answers are more precise and less vague. I am about to find the solution and post it here. – Will de la Vega Mar 10 '13 at 07:33
  • Thanks, I just did. I am going to edit my post, so it contributes to the answer of this issue. – Will de la Vega Mar 10 '13 at 07:53