Permalink
Browse files

Making it possible to cancel the render() process from any beforeRend…

…er listener
  • Loading branch information...
1 parent b79e0ad commit 28ee27e2dd0ae16ee5f2fbdc725bb48af4c27986 @lorenzo lorenzo committed Jan 19, 2012
Showing with 29 additions and 1 deletion.
  1. +6 −1 lib/Cake/Controller/Controller.php
  2. +23 −0 lib/Cake/Test/Case/Controller/ControllerTest.php
@@ -896,7 +896,12 @@ public function validateErrors() {
* @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') {
@@ -344,6 +344,14 @@ public function beforeRender($controller) {
}
}
+class Test2Component extends TestComponent {
+
+
+ public function beforeRender($controller) {
+ return false;
+ }
+}
+
/**
* AnotherTestController class
*
@@ -670,6 +678,21 @@ public function testComponentBeforeRenderChangingViewClass() {
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
*

0 comments on commit 28ee27e

Please sign in to comment.