我有我的控制器類擴展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