Skip to content

Commit

Permalink
Re-fetch View class to pass view variables set in beforeRender callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
Marlinc committed Aug 11, 2015
1 parent c79a106 commit dbd0672
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/Controller/Controller.php
Expand Up @@ -572,6 +572,9 @@ public function render($view = null, $layout = null)
return $this->response;
}

// Re-fetch View class to pass view variables set in beforeRender callbacks
$this->View = $this->getView();

if ($viewClass !== $this->viewClass) {
$this->View = $this->getView($this->viewClass);
if (!$this->_view->viewPath()) {
Expand Down
22 changes: 22 additions & 0 deletions tests/TestCase/Controller/ControllerTest.php
Expand Up @@ -28,6 +28,7 @@
use Cake\TestSuite\TestCase;
use Cake\Utility\ClassRegistry;
use Cake\Utility\Hash;
use TestApp\Controller\Admin\PostsController;
use TestPlugin\Controller\TestPluginController;

/**
Expand Down Expand Up @@ -968,4 +969,25 @@ public function testDeclaredDeprecatedProperty()
$this->assertEquals($theme, @$controller->getView()->theme);
// @codingStandardsIgnoreEnd
}

/**
* Test that view variables are being set after the beforeRender event gets dispatched
*
* @return void
*/
public function testBeforeRenderViewVariables()
{
$controller = new PostsController();

$controller->eventManager()->on('Controller.beforeRender', function (Event $event) {
/* @var Controller $controller */
$controller = $event->subject();

$controller->set('testVariable', 'test');
});

$controller->render('index');

$this->assertArrayHasKey('testVariable', $controller->View->viewVars);
}
}

0 comments on commit dbd0672

Please sign in to comment.