2

I just wanted to ask why the following inside a Zend_Controller_Action action method:

$request = $this->getRequest();
$params = $request->getParams();
var_dump($params);
foreach ($params as $key => &$value) {
    $value = null;
}
var_dump($params);
$request->setParams($params);
var_dump($request->getParams());

produces this:

array
  'controller' => string 'bug' (length=3)
  'action' => string 'edit' (length=4)
  'id' => string '210' (length=3)
  'module' => string 'default' (length=7)
  'author' => string 'test2' (length=5)

array
  'controller' => null
  'action' => null
  'id' => null
  'module' => null
  'author' => null

array
  'author' => string 'test2' (length=5)

Shouldn't the 'author' variable be cleared too?

Thanks in advance!

David Snabel-Caunt
  • 57,804
  • 13
  • 114
  • 132
krzysiek
  • 465
  • 5
  • 16

2 Answers2

1

To clear params you can simply call:

$request->clearParams(); 
Somnath Muluk
  • 55,015
  • 38
  • 216
  • 226
Bartosz Rychlicki
  • 1,918
  • 3
  • 20
  • 41
1

The getParams method is shown below. What happens is you're clearing the built in params (controller, action etc...) but the method always returns GET and POST variables.

/**
 * Retrieve an array of parameters
 *
 * Retrieves a merged array of parameters, with precedence of userland
 * params (see {@link setParam()}), $_GET, $_POST (i.e., values in the
 * userland params will take precedence over all others).
 *
 * @return array
 */
public function getParams()
{
    $return       = $this->_params;
    $paramSources = $this->getParamSources();
    if (in_array('_GET', $paramSources)
        && isset($_GET)
        && is_array($_GET)
    ) {
        $return += $_GET;
    }
    if (in_array('_POST', $paramSources)
        && isset($_POST)
        && is_array($_POST)
    ) {
        $return += $_POST;
    }
    return $return;
}
David Snabel-Caunt
  • 57,804
  • 13
  • 114
  • 132
  • is there something like a clearPost method? setPost does not accept null values :/ – krzysiek Sep 27 '11 at 16:47
  • You can use $request->setParamSources(array()); which affects the $paramSources above - if it's set to an empty array, GET and POST data is not retrieved. You may need to set it back to including GET and POST if other code relies on getParams. – David Snabel-Caunt Sep 27 '11 at 16:55
  • I meant like clearing all POST and GET data in the Request Object. I wrote them myself already, but maybe there is some other way, how things are ment to be done by Zend Developers? – krzysiek Sep 27 '11 at 17:18