使用Zend Framework 1.12,只需使用getParam()方法即可。注意getParam()不只的结果为NULL为没有可用的键,一个串1键和阵列,用于多个键:
否 'id' 值
http://domain.com/module/controller/action/
$id = $this->getRequest()->getParam('id');
// NULL
单 'id' 值
http://domain.com/module/controller/action/id/122
$id = $this->getRequest()->getParam('id');
// string(3) "122"
多“身份证”值:
http://domain.com/module/controller/action/id/122/id/2584
$id = $this->getRequest()->getParam('id');
// array(2) { [0]=> string(3) "122" [1]=> string(4) "2584" }
这可能是有问题的,如果你总是希望在你的代码串,出于某种原因,更值在URL中设置:在某些情况下,例如,你可以运行进入错误“Array to string conversion”。这里有一些技巧,以避免这样的错误,以确保您始终获得结果你getParam()不只需要的类型:
如果你想在$ ID是一个阵列(或NULL如果param未设置)
$id = $this->getRequest()->getParam('id');
if($id !== null && !is_array($id)) {
$id = array($id);
}
http://domain.com/module/controller/action/
// NULL
http://domain.com/module/controller/action/id/122
// array(1) { [0]=> string(3) "122" }
如果总是希望的$ id是一个阵列(没有NULL如果没有设置值,只是空数组):
$id = $this->getRequest()->getParam('id');
if(!is_array($id)) {
if($id === null) {
$id = array();
} else {
$id = array($id);
}
}
http://domain.com/module/controller/action/
// array(0) { }
http://domain.com/module/controller/action/id/122
// array(1) { [0]=> string(3) "122" }
山姆Ë如上面一行(没有NULL,始终阵列):
$id = (array)$this->getRequest()->getParam('id');
如果你想在$ ID是一个字符串(在第一个可用值,保持NULL完好)
$id = $this->getRequest()->getParam('id');
if(is_array($id)) {
$id = array_shift(array_values($id));
}
http://domain.com/module/controller/action/
// NULL
http://domain.com/module/controller/action/122/id/2584
// string(3) "122"
如果你想在$ ID是一个字符串(在最后一个可用值,保持完好NULL)
$id = $this->getRequest()->getParam('id');
if(is_array($id)) {
$id = array_pop(array_values($id));
}
http://domain.com/module/controller/action/
// NULL
http://domain.com/module/controller/action/122/id/2584/id/52863
// string(5) "52863"
也许有更短的/更好的方法来解决这些getParam的'响应类型',但是如果你要使用上面的脚本,可以更清洁地为它创建另一个方法(扩展的助手或其他)。