2012-09-01 44 views
5

Tôi tự hỏi tại sao không ai hỏi câu hỏi này.zend: va chạm tham số

Mỗi chức năng hành động zend trong lớp điều khiển có 3 tham số, cụ thể là 'mô-đun', 'bộ điều khiển' và 'hành động'.

Điều gì xảy ra, khi tôi nhận được thông số có tên là 'hành động' từ một biểu mẫu hoặc url, ví dụ: "? Action = edit" ??

Tôi đã thử nghiệm: hành động giữ giá trị của nó từ bộ định tuyến chứ không phải 'chỉnh sửa'.

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

Làm cách nào để chuyển thông số có tên "hành động", nếu tôi phải làm như vậy ??

Xin cảm ơn trước.

Trả lời

2

Các tuyến đường mặc định là Zend_Controller_Router_Route_Module trong đó sử dụng các phím mặc định cho module, điều khiển, & hành động:

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; 
} 

Bạn có thể thấy rằng con đường module mặc định có các phím mặc định cho params MVC, tuy nhiên, nó sẽ sử dụng các phím được đặt bởi đối tượng yêu cầu nếu nó tồn tại và chúng ta có thể sửa đổi các khóa này.

ví dụ: trong bootstrap của bạn:

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); 
    } 
} 

Bây giờ bạn có thể sử dụng mô-đun , khiển, & hành động như $_GET params.

1

Sau một chút thử nghiệm, có vẻ như cách bạn chuyển khóa quan trọng "hành động" vấn đề.

Nếu bạn thử và chuyển một thông số có tên "hành động" bằng $this->_request->getParams() bạn sẽ nhận được cặp khóa giá trị hành động của bộ điều khiển.

Nếu bạn vượt qua "hành động" quan trọng từ một hình thức với $form->getValues() bạn sẽ lấy giá trị từ các yếu tố hình thức đặt tên "hành động".

Cũng như rất nhiều thứ, trường hợp sử dụng của bạn xác định cách bạn cần xử lý tình huống.

Chúc may mắn.

Các vấn đề liên quan