19

I want to fetch posted data. But I am using no form. The data is postet by a jquery script with method post and I would like to fetch it.

I know how to fetch parameters

$id = $this->getRequest ()->getParam ( 'id', null );

and form values

$message = $form->getValue ( 'message' );

however I want to access post data and not parameters or form values. Any ideas?

DarkLeafyGreen
  • 69,338
  • 131
  • 383
  • 601

4 Answers4

59

Here is my solution;)

$this->getRequest()->getPost('id', null);
DarkLeafyGreen
  • 69,338
  • 131
  • 383
  • 601
  • 14
    NULL is the default value, no need to specify it. – takeshin Sep 19 '10 at 14:45
  • 6
    Typical ZF2 - found what I was looking for here but not in the ZF2 documentation.. Thanks – someuser Feb 05 '13 at 08:02
  • if nothing is passed to getPost, then it will return all the data. It will act like $_POST. Just used it and it worked fine. Thanks for sharing. – Altaf Hussain Dec 23 '13 at 11:29
  • How to have Lazy mode on? like $post= $this->getRequest(); and then anywhere access it like $post->username where username is the $_POST['username'] ? –  Jan 29 '17 at 10:59
18

Actually, this might be more of what you're looking for.

$this->getRequest()->getRawBody();

https://framework.zend.com/manual/1.12/en/zend.controller.request.html

Richard Ayotte
  • 5,021
  • 1
  • 36
  • 34
7

Here is an other example:

$this->getRequest()->getPost()->toArray()
  • 2
    `$this->getRequest()->getPost()` seems to already be an array, and thus calling `toArray()` on it results in an error. This is what I needed though, +1 – Shautieh Mar 29 '17 at 12:46
2

Try this:

$request = $this->getRequest();
$request->getPost('field_name');
Utsav Dawn
  • 7,896
  • 2
  • 29
  • 44
dhirendra
  • 21
  • 1
  • While this answer is probably correct and useful, it is preferred if you include some explanation along with it to explain how it helps to solve the problem. This becomes especially useful in the future, if there is a change (possibly unrelated) that causes it to stop working and users need to understand how it once worked. – Kevin Brown-Silva Mar 06 '15 at 00:07