2012-09-01 57 views
5

我想知道为什么没有人问过这个问题。zend:参数冲突

控制器类中的每个zend Action函数都有3个参数,分别是'module','controller'和'action'。

当我从一个表单或url获得一个名为'action'的参数时会发生什么,例如“?action = edit”?

我测试了它:action从路由器保存它的值,而不是'编辑'。

public function someAction() { 
    $params = $this->getRequest()->getParams(); 
... 

我该如何传递名为“action”的参数,如果我不得不?

在此先感谢。

回答

2

缺省路由是Zend_Controller_Router_Route_Module它使用的默认键模块,控制器,&行动:

protected $_moduleKey  = 'module'; 
protected $_controllerKey = 'controller'; 
protected $_actionKey  = 'action'; 

// ... 

/** 
* Set request keys based on values in request object 
* 
* @return void 
*/ 
protected function _setRequestKeys() 
{ 
    if (null !== $this->_request) { 
     $this->_moduleKey  = $this->_request->getModuleKey(); 
     $this->_controllerKey = $this->_request->getControllerKey(); 
     $this->_actionKey  = $this->_request->getActionKey(); 
    } 

    if (null !== $this->_dispatcher) { 
     $this->_defaults += array(
      $this->_controllerKey => $this->_dispatcher->getDefaultControllerName(), 
      $this->_actionKey  => $this->_dispatcher->getDefaultAction(), 
      $this->_moduleKey  => $this->_dispatcher->getDefaultModule() 
     ); 
    } 

    $this->_keysSet = true; 
} 

/** 
* Matches a user submitted path. Assigns and returns an array of variables 
* on a successful match. 
* 
* If a request object is registered, it uses its setModuleName(), 
* setControllerName(), and setActionName() accessors to set those values. 
* Always returns the values as an array. 
* 
* @param string $path Path used to match against this routing map 
* @return array An array of assigned values or a false on a mismatch 
*/ 
public function match($path, $partial = false) 
{ 
    $this->_setRequestKeys(); 

    $values = array(); 
    $params = array(); 

    if (!$partial) { 
     $path = trim($path, self::URI_DELIMITER); 
    } else { 
     $matchedPath = $path; 
    } 

    if ($path != '') { 
     $path = explode(self::URI_DELIMITER, $path); 

     if ($this->_dispatcher && $this->_dispatcher->isValidModule($path[0])) { 
      $values[$this->_moduleKey] = array_shift($path); 
      $this->_moduleValid = true; 
     } 

     if (count($path) && !empty($path[0])) { 
      $values[$this->_controllerKey] = array_shift($path); 
     } 

     if (count($path) && !empty($path[0])) { 
      $values[$this->_actionKey] = array_shift($path); 
     } 

     if ($numSegs = count($path)) { 
      for ($i = 0; $i < $numSegs; $i = $i + 2) { 
       $key = urldecode($path[$i]); 
       $val = isset($path[$i + 1]) ? urldecode($path[$i + 1]) : null; 
       $params[$key] = (isset($params[$key]) ? (array_merge((array) $params[$key], array($val))): $val); 
      } 
     } 
    } 

    if ($partial) { 
     $this->setMatchedPath($matchedPath); 
    } 

    $this->_values = $values + $params; 

    return $this->_values + $this->_defaults; 
} 

您可以看到默认的模块路径有MVC PARAMS默认键,但是,它将使用键如果它存在,则由请求对象设置,我们可以修改这些键。

例如在你的引导:

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap 
{ 
    protected function _initRequestKeys() 
    { 
     $this->bootstrap('frontcontroller'); 
     $frontController = $this->getResource('frontcontroller'); 
     /* @var $frontController Zend_Controller_Front */ 

     $request = new Zend_Controller_Request_Http(); 

     // change action key 
     $request->setActionKey("new_action_key"); 

     // change module 
     $request->setModuleKey("new_module_key"); 

     // change controller 
     $request->setControllerKey("new_controller_key"); 


     // don't forget to set the configured request 
     // object to the front controller 
     $frontController->setRequest($request); 
    } 
} 

现在你可以使用模块控制器,& 行动$_GET PARAMS。

1

经过一点测试,似乎你通过钥匙“行动”事情。

如果您尝试传递一个名为“动作”$this->_request->getParams()的参数,您将获得控制器动作值密钥对。

如果从$form->getValues()形式传递“行动”键,你将会从命名“行动”表单元素的值。

与许多事情一样,您的用例决定了您需要如何处理这种情况。

祝你好运。