Created
February 20, 2014 05:56
-
-
Save simkimsia/9107770 to your computer and use it in GitHub Desktop.
Jose original code from http://josediazgonzalez.com/2013/12/12/abusing-exceptions-to-provide-model-layer-redirection/
This file contains hidden or 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 | |
| App::uses('AppException', 'Lib'); | |
| App::uses('ErrorHandler', 'Error'); | |
| class AppErrorHandler extends ErrorHandler { | |
| public static function handleException(Exception $exception) { | |
| if ($exception instanceof AppException) { | |
| $element = 'default'; | |
| $message = $exception->getMessage(); | |
| $params = array('class' => 'error'); | |
| CakeSession::write('Message.flash', compact('message', 'element', 'params')); | |
| if ($exception->hasRoute()) { | |
| $controller = $this->_getController(); | |
| return $controller->redirect($exception->getRoute()); | |
| } | |
| } | |
| return parent::handleException($exception); | |
| } | |
| /** | |
| * 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. | |
| * | |
| * @param Exception $exception The exception to get a controller for. | |
| * @return Controller | |
| */ | |
| protected function _getController($exception) { | |
| App::uses('CakeErrorController', 'Controller'); | |
| if (!$request = Router::getRequest(true)) { | |
| $request = new CakeRequest(); | |
| } | |
| $response = new CakeResponse(array('charset' => Configure::read('App.encoding'))); | |
| try { | |
| if (class_exists('AppController')) { | |
| $controller = new CakeErrorController($request, $response); | |
| } | |
| } catch (Exception $e) { | |
| } | |
| if (empty($controller)) { | |
| $controller = new Controller($request, $response); | |
| $controller->viewPath = 'Errors'; | |
| } | |
| return $controller; | |
| } | |
| } | |
| ?> |
This file contains hidden or 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 | |
| // app /Lib/Exception/AppException.php | |
| class AppException extends CakeException { | |
| public function <strong>construct($message, $code = 0) { | |
| parent::</strong>construct($message, $code); | |
| return $this; | |
| }<p></p> | |
| public function setRoute($route = null) { | |
| $this->_attributes['route'] = $route; | |
| return $this; | |
| } | |
| } | |
| ?> |
This file contains hidden or 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 | |
| // app /Lib/Exception/AppException.php | |
| class AppException extends CakeException { | |
| public function setRoute($route = null) { | |
| $this->_attributes['route'] = $route; | |
| } | |
| public function getRoute() { | |
| return $this->_attributes['route']; | |
| } | |
| public function hasRoute() { | |
| return isset($this->_attributes['route']); | |
| } | |
| } | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment