From cfbc43671ea19535a4356ac164405f280d1213b3 Mon Sep 17 00:00:00 2001 From: mark_story Date: Fri, 16 Sep 2011 21:52:12 -0400 Subject: [PATCH] Starting content type specific error pages. - Adding RequestHandler to the error controller. This allows reuse of all of Cake's internals. - Adding a simple JsonView class to do serialized JSON views. - Adding serialize hooks, and wiring things together. --- lib/Cake/Controller/CakeErrorController.php | 8 ++++++ .../Component/RequestHandlerComponent.php | 7 ++++- lib/Cake/Error/ExceptionRenderer.php | 6 ++++- lib/Cake/View/JsonView.php | 27 +++++++++++++++++++ 4 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 lib/Cake/View/JsonView.php diff --git a/lib/Cake/Controller/CakeErrorController.php b/lib/Cake/Controller/CakeErrorController.php index fea081b61..d38118a21 100644 --- a/lib/Cake/Controller/CakeErrorController.php +++ b/lib/Cake/Controller/CakeErrorController.php @@ -42,9 +42,17 @@ class CakeErrorController extends AppController { */ public function __construct($request = null, $response = null) { parent::__construct($request, $response); + if (count(Router::extensions())) { + $this->components[] = 'RequestHandler'; + } $this->constructClasses(); $this->Components->trigger('initialize', array(&$this)); + $this->_set(array('cacheAction' => false, 'viewPath' => 'Errors')); + + if (isset($this->RequestHandler)) { + $this->RequestHandler->startup($this); + } } /** diff --git a/lib/Cake/Controller/Component/RequestHandlerComponent.php b/lib/Cake/Controller/Component/RequestHandlerComponent.php index dbdaa8dd6..e4408098b 100644 --- a/lib/Cake/Controller/Component/RequestHandlerComponent.php +++ b/lib/Cake/Controller/Component/RequestHandlerComponent.php @@ -556,7 +556,12 @@ class RequestHandlerComponent extends Component { } $controller->ext = '.ctp'; - if (empty($this->_renderType)) { + $viewClass = ucfirst($type); + App::uses($viewClass . 'View', 'View'); + + if (class_exists($viewClass . 'View')) { + $controller->viewClass = $viewClass; + } elseif (empty($this->_renderType)) { $controller->viewPath .= DS . $type; } else { $remove = preg_replace("/([\/\\\\]{$this->_renderType})$/", DS . $type, $controller->viewPath); diff --git a/lib/Cake/Error/ExceptionRenderer.php b/lib/Cake/Error/ExceptionRenderer.php index a2de723dd..a9d2f8c9f 100644 --- a/lib/Cake/Error/ExceptionRenderer.php +++ b/lib/Cake/Error/ExceptionRenderer.php @@ -181,6 +181,7 @@ class ExceptionRenderer { 'url' => h($url), 'name' => $error->getMessage(), 'error' => $error, + 'serialize' => array('code', 'error', 'name', 'url') )); try { $this->controller->set($error->getAttributes()); @@ -208,7 +209,8 @@ class ExceptionRenderer { $this->controller->set(array( 'name' => $message, 'url' => h($url), - 'error' => $error, + 'error' => $error, + 'serialize' => array('error', 'name', 'url') )); $this->_outputMessage('error400'); } @@ -231,6 +233,7 @@ class ExceptionRenderer { 'name' => $message, 'message' => h($url), 'error' => $error, + 'serialize' => array('error', 'name', 'url') )); $this->_outputMessage('error500'); } @@ -250,6 +253,7 @@ class ExceptionRenderer { 'url' => h($url), 'name' => $error->getMessage(), 'error' => $error, + 'serialize' => array('code', 'error', 'name', 'url') )); try { $this->_outputMessage($this->template); diff --git a/lib/Cake/View/JsonView.php b/lib/Cake/View/JsonView.php new file mode 100644 index 000000000..a058d03a1 --- /dev/null +++ b/lib/Cake/View/JsonView.php @@ -0,0 +1,27 @@ +viewVars['serialize'])) { + $vars = array_intersect_key( + $this->viewVars, + array_flip($this->viewVars['serialize']) + ); + return json_encode($vars); + } + return parent::render($view, $layout); + } +}