2

我只是想问一下为什么在 Zend_Controller_Action 操作方法中有以下内容:

$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());

产生这个:

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)

'author' 变量不应该也被清除吗?

提前致谢!

4

2 回答 2

1

getParams 方法如下所示。发生的情况是您正在清除内置参数(控制器、操作等),但该方法始终返回 GET 和 POST 变量。

/**
 * 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;
}
于 2011-09-27T15:45:57.997 回答
1

要清除参数,您只需调用:

$request->clearParams(); 
于 2012-05-08T11:49:12.637 回答