views:

616

answers:

3

We are using the Zend Router and it seems that its overwriting the parameters that are sent by forms. The only parameters that arrive to the controller are the params from the Url.

Does anyone know why this is happening?

Here is the config file:

; Routing config

routes.groups.route = groups/:group/:type/:idPost/:postUrl/:page
routes.groups.defaults.controller = groups
routes.groups.defaults.action = index
routes.groups.defaults.type = main
routes.groups.defaults.idPost = 
routes.groups.defaults.postUrl = 
routes.groups.defaults.page = 1

And the form:

<form action="<?= $this->_view->baseUrl ?>/groups/<?= $group['groupUrl'] ?>/deletepost/" method="post">
<input type="hidden" name="formUrl" value="<?=$formUrl ?> />
  ...
</form>

Controller:

public function deletepostAction() {
    $params = $this->getRequest()->getParams();
    print_r($params);
    die;
}

...that outputs:

Array
(
   [group] => dandy-handwriting
   [type] => deletepost
   [idPost] => 
   [controller] => groups
   [action] => index
   [postUrl] => 
   [idGroup] => 1
   [lang] => en
)

note that "formUrl" is missing, its only the parameters from the router.

+1  A: 

Probably you are sending your form data as GET, and have configured Zend_Router to rewrite the url (without taking the other get parameters).

In that case the solution is to send the form data with POST or change the routes in Zend_Router.

Your code would help to determine what your exact problem is.

Peter Smit
+3  A: 

You can use the request-object in your controller to access your data.

Fetch the request object: $request = $this->getRequest();

Retrieve POST data (if your form is submitted via POST): $post = $request->getPost();

Retrieve GET data (if your form is submitted via GET): $get = $request->getQuery();

Retrieve parameter in the order user parameters set via setParam(), GET parameters and POST parameters: $params = $request->getParams();

If you fetch your data with getParams() the params set by the router will override your POST data.

So if you only want to fetch the data from your form, use the getPost() or getQuery() method.

Flo
Thanks for the answer. We would like to send the form data as POST though, for actions like addComment and addPost. Do you know if there is a way to make the Router send the parameters by GET?
lasse
The router does not send parameters via either GET or POST. It simply injects them into the request using its setParam* family of methods.Flo is completely right, you simply need to only get the POST params from the request object when processing your form, and everything will be great.
jason
Thanks for clarifying jason. Thanks again, Flo, for the answer. Now that i read it again it makes a lot of sense. This was late on a friday and my brain wasn't following well.
lasse
A: 

Accidently posted as answer.