Created
November 23, 2012 20:07
-
-
Save angelxmoreno/4137074 to your computer and use it in GitHub Desktop.
Workaround for CakePHP bug #3389 ExceptionRenderer uses the wrong viewClass
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Exception Renderer Fixed | |
* Angel S. Moreno (angelxmoreno@gmail.com) | |
* | |
* As per ticket #3389 (http://cakephp.lighthouseapp.com/projects/42648/tickets/3389-exceptionrenderer-uses-the-wrong-viewclass) | |
* the ExceptionRenderer fails to use the proper view class when an exception is thrown in the AppController's beforeFilter. | |
* This new ExceptionRenderer extends the original and updates the view class based on RequestHandlerComponent::params['ext'] and | |
* Router::extensions(). It assumes that when RequestHandlerComponent::params['ext'] is set and the extention is in the array of | |
* Router::extensions(), that the viewClass to use is prefixed by the extension being used. That being said, when the extenion is | |
* xml the xmlView would be used. | |
* | |
* Instalation: | |
* 1. Drop this file in your App/Lib/Error/ directory | |
* 2. Edit your App/core.php file to use this Class to render your exceptions | |
* Configure::write('Exception', array( | |
* 'handler' => 'ErrorHandler::handleException', | |
* 'renderer' => 'ExceptionRendererFixed', | |
* 'log' => true | |
* )); | |
*/ | |
App::uses('ExceptionRenderer', 'Error'); | |
class ExceptionRendererFixed extends ExceptionRenderer { | |
/** | |
* Get the controller instance to handle the exception. | |
* Override this method in subclasses to customize the controller used. | |
* This method returns the built in `CakeErrorController` normally, or if an error is repeated | |
* a bare controller will be used. Additionally, The method attempts to identify the proper viewClass | |
* to use and sets it as the Coontroller::viewClass property. | |
* | |
* @param Exception $exception The exception to get a controller for. | |
* @return Controller | |
*/ | |
protected function _getController($exception) { | |
$controller = parent::_getController($exception); | |
return $this->_fixViewClassProperty($controller, $this->_getRequest()); | |
} | |
/** | |
* @param Controller $controller The controller to update | |
* @param CakeRequest $request a CakeRequest object to determine the viewClass to use | |
* @return Controller | |
*/ | |
protected function _fixViewClassProperty(Controller $controller, CakeRequest $request) { | |
if (!empty($request->params['ext']) and in_array($request->params['ext'], Router::extensions())) { | |
$controller->viewClass = $request->params['ext']; | |
} | |
return $controller; | |
} | |
/** | |
* Get the either a request object from the top of the stack or create a new one | |
* | |
* @return CakeRequest | |
*/ | |
protected function _getRequest() { | |
if (!$request = Router::getRequest(true)) { | |
$request = new CakeRequest(); | |
} | |
return $request; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment