Get post data, Zend Framework
Asked Answered
P

4

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?

Principe answered 19/9, 2010 at 13:32 Comment(0)
P
59

Here is my solution;)

$this->getRequest()->getPost('id', null);
Principe answered 19/9, 2010 at 14:27 Comment(4)
NULL is the default value, no need to specify it.Clotho
Typical ZF2 - found what I was looking for here but not in the ZF2 documentation.. ThanksMiscount
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.Loeffler
How to have Lazy mode on? like $post= $this->getRequest(); and then anywhere access it like $post->username where username is the $_POST['username'] ?Britzka
B
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

Brandy answered 24/3, 2011 at 13:36 Comment(2)
Is there a way to get them as array(key value pairs).Tropous
@Tropous please expand; I'm not sure I understand your question.Brandy
A
7

Here is an other example:

$this->getRequest()->getPost()->toArray()
Aircrewman answered 16/1, 2015 at 16:59 Comment(1)
$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, +1Limacine
B
2

Try this:

$request = $this->getRequest();
$request->getPost('field_name');
Ballyhoo answered 5/3, 2015 at 18:29 Comment(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.Estaminet

© 2022 - 2024 — McMap. All rights reserved.