mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
Making it possible to cancel the render() process from any beforeRender listener
This commit is contained in:
parent
b79e0ad8f3
commit
28ee27e2dd
2 changed files with 29 additions and 1 deletions
|
@ -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') {
|
||||
|
|
|
@ -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
|
||||
*
|
||||
|
|
Loading…
Reference in a new issue