Skip to content

Commit

Permalink
Merge pull request #1086 from burzum/refactor/controller-render
Browse files Browse the repository at this point in the history
Refactoring the Controller::render method

Create Controller::_getViewObject() which is responsible for creating
the view instance. This gives an easier way to override view construction.
  • Loading branch information
markstory committed Jan 28, 2013
2 parents bb76740 + d269b28 commit 7a18470
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions lib/Cake/Controller/Controller.php
Expand Up @@ -930,14 +930,7 @@ public function render($view = null, $layout = null) {
}
}

$viewClass = $this->viewClass;
if ($this->viewClass != 'View') {
list($plugin, $viewClass) = pluginSplit($viewClass, true);
$viewClass = $viewClass . 'View';
App::uses($viewClass, $plugin . 'View');
}

$View = new $viewClass($this);
$this->View = $this->_getViewObject();

$models = ClassRegistry::keys();
foreach ($models as $currentModel) {
Expand All @@ -946,13 +939,12 @@ public function render($view = null, $layout = null) {
$className = get_class($currentObject);
list($plugin) = pluginSplit(App::location($className));
$this->request->params['models'][$currentObject->alias] = compact('plugin', 'className');
$View->validationErrors[$currentObject->alias] =& $currentObject->validationErrors;
$this->View->validationErrors[$currentObject->alias] =& $currentObject->validationErrors;
}
}

$this->autoRender = false;
$this->View = $View;
$this->response->body($View->render($view, $layout));
$this->response->body($this->View->render($view, $layout));
return $this->response;
}

Expand Down Expand Up @@ -1224,4 +1216,20 @@ protected function _scaffoldError($method) {
return $this->scaffoldError($method);
}

/**
* Constructs the view class instance based on the controller property
*
* @return View
*/
protected function _getViewObject() {
$viewClass = $this->viewClass;
if ($this->viewClass !== 'View') {
list($plugin, $viewClass) = pluginSplit($viewClass, true);
$viewClass = $viewClass . 'View';
App::uses($viewClass, $plugin . 'View');
}

return new $viewClass($this);
}

}

0 comments on commit 7a18470

Please sign in to comment.