开发者

Zend Framework: Getting an empty param

开发者 https://www.devze.com 2022-12-15 04:51 出处:网络
Lets say I have the following Request URI: /controller/action/filter// When I call $this->_getParam(\'filter\') in the controller, it will return NULL. However, I want it to return an em开发者_如何学

Lets say I have the following Request URI: /controller/action/filter//

When I call $this->_getParam('filter') in the controller, it will return NULL. However, I want it to return an em开发者_如何学Pythonpty string. Am I missing something obvious?


_getParam() can take a second optional argument holding a default return value. See the note here: http://framework.zend.com/manual/en/zend.controller.action.html#zend.controller.action.accessors

So $this->_getParam('filter','') should probably do what you need.

Edit: thanks for the clarification, gotcha. Not sure sure there's a particularly clean way to do what you want - looking at the code for Zend_Controller_Request_Abstract I can see it explicitly deletes the key from the parameter list if it has no value, so there's no easy way to tell if it was ever there from the controller. Something like the following will do the job, but gets a little ugly (you might be better off using an explicit 'reset' value for your filter parameter so you can capture it in a more sane fashion, rather than just an empty string, if that's possible?).

// if '/filter/' is in the request URI, but has no value
if (strpos($_SERVER["REQUEST_URI"],'/filter/') && !$this->_hasParam('filter'))
{
  // code to reset filter
}
// if filter is in the request *and* has a value
elseif ($this->_hasParam('filter'))
{
  // code to use filter value from request
}
else
{
  // code to get filter from session
}
0

精彩评论

暂无评论...
验证码 换一张
取 消