2014-10-12 17 views

回答

0

您應該從控制器中粘貼代碼,以便人們可以確切地看到您在說什麼。

假設你有一個非常標準的編輯方法,它應該包含一條線是這樣的:

$this->request->data = $this->Post->read(null, $id)

因此,$this->request->data設置。蛋糕確保整個$this->request在視圖中可用。在View超類中,有一個名爲$_passedVars的數組,其中包括request。在視圖的__construct中,它將控制器中$_passedVars中指定的每個變量複製到視圖中。

如果你想看到的細節,請/lib/Cake/View/View.php,特別是$_passedVars變量,__construct方法的前幾行:

/** 
* List of variables to collect from the associated controller. 
* 
* @var array 
*/ 
    protected $_passedVars = array(
     'viewVars', 'autoLayout', 'ext', 'helpers', 'view', 'layout', 'name', 'theme', 
     'layoutPath', 'viewPath', 'request', 'plugin', 'passedArgs', 'cacheAction' 
    ); 

/** 
* Constructor 
* 
* @param Controller $controller A controller object to pull View::_passedVars from. 
*/ 
    public function __construct(Controller $controller = null) { 
     if (is_object($controller)) { 
      $count = count($this->_passedVars); 
      for ($j = 0; $j < $count; $j++) { 
       $var = $this->_passedVars[$j]; 
       $this->{$var} = $controller->{$var}; 
      } 
      $this->_eventManager = $controller->getEventManager(); 
     } 
     if (empty($this->request) && !($this->request = Router::getRequest(true))) { 
      $this->request = new CakeRequest(null, false); 
      $this->request->base = ''; 
      $this->request->here = $this->request->webroot = '/'; 
     } 
     if (is_object($controller) && isset($controller->response)) { 
      $this->response = $controller->response; 
     } else { 
      $this->response = new CakeResponse(); 
     } 
     $this->Helpers = new HelperCollection($this); 
     $this->Blocks = new ViewBlock(); 
     $this->loadHelpers(); 
     parent::__construct(); 
    }