我有我的控制器类扩展Zend_Controller_Action
并进行了如下修改:
在dispatch($action)
方法代替
$this->$action();
与
call_user_func_array(array($this,$action), $this->getUrlParametersByPosition());
而在我的行动增加了以下方法
/**
* Returns array of url parts after controller and action
*/
protected function getUrlParametersByPosition()
{
$request = $this->getRequest();
$path = $request->getPathInfo();
$path = explode('/', trim($path, '/'));
if(@$path[0]== $request->getControllerName())
{
unset($path[0]);
}
if(@$path[1] == $request->getActionName())
{
unset($path[1]);
}
return $path;
}
现在的URL像/mycontroller/myaction/123/321
我会得到以下所有控制器的PARAMS和行动
public function editAction($param1 = null, $param2 = null)
{
// $param1 = 123
// $param2 = 321
}
URL中的其他参数不会导致任何错误,因为您可以将更多参数发送给随后定义的方法。您可以通过func_get_args()
获得所有这些信息,而且您仍可以通常的方式使用getParam()
。 您的网址可能不包含使用默认网址的操作名称。
其实我的网址不包含参数名称。只有他们的价值。 (正如它在问题中一样) 而且您必须定义路径以指定URL中的参数位置,以遵循框架的概念并能够使用Zend方法构建URL。 但是如果你总是知道你的参数在URL中的位置,你可以很容易地得到它。
这并不像使用反射方法那么复杂,但我想提供更少的开销。
调度方法现在看起来是这样的:
/**
* Dispatch the requested action
*
* @param string $action Method name of action
* @return void
*/
public function dispatch($action)
{
// Notify helpers of action preDispatch state
$this->_helper->notifyPreDispatch();
$this->preDispatch();
if ($this->getRequest()->isDispatched()) {
if (null === $this->_classMethods) {
$this->_classMethods = get_class_methods($this);
}
// preDispatch() didn't change the action, so we can continue
if ($this->getInvokeArg('useCaseSensitiveActions') || in_array($action, $this->_classMethods)) {
if ($this->getInvokeArg('useCaseSensitiveActions')) {
trigger_error('Using case sensitive actions without word separators is deprecated; please do not rely on this "feature"');
}
//$this->$action();
call_user_func_array(array($this,$action), $this->getUrlParametersByPosition());
} else {
$this->__call($action, array());
}
$this->postDispatch();
}
// whats actually important here is that this action controller is
// shutting down, regardless of dispatching; notify the helpers of this
// state
$this->_helper->notifyPostDispatch();
}
尼斯,该文档是不是在考虑那么清楚。 – 2010-04-13 07:04:58