Making it possible to cancel the render() process from any beforeRender listener

This commit is contained in:
Jose Lorenzo Rodriguez 2012-01-19 00:03:36 -04:30
parent b79e0ad8f3
commit 28ee27e2dd
2 changed files with 29 additions and 1 deletions

View file

@ -896,7 +896,12 @@ class Controller extends Object implements CakeEventListener {
* @link http://book.cakephp.org/2.0/en/controllers.html#Controller::render
*/
public function render($view = null, $layout = null) {
$this->getEventManager()->dispatch(new CakeEvent('Controller.beforeRender', $this));
$event = new CakeEvent('Controller.beforeRender', $this);
$this->getEventManager()->dispatch($event);
if ($event->isStopped()) {
$this->autoRender = false;
return $this->response;
}
$viewClass = $this->viewClass;
if ($this->viewClass != 'View') {

View file

@ -344,6 +344,14 @@ class TestComponent extends Object {
}
}
class Test2Component extends TestComponent {
public function beforeRender($controller) {
return false;
}
}
/**
* AnotherTestController class
*
@ -670,6 +678,21 @@ class ControllerTest extends CakeTestCase {
App::build();
}
/**
* test that a component beforeRender can change the controller view class.
*
* @return void
*/
public function testComponentCancelRender() {
$Controller = new Controller($this->getMock('CakeRequest'), new CakeResponse());
$Controller->uses = array();
$Controller->components = array('Test2');
$Controller->constructClasses();
$result = $Controller->render('index');
$this->assertInstanceOf('CakeResponse', $result);
}
/**
* testToBeInheritedGuardmethods method
*