Skip to content

Commit

Permalink
Starting content type specific error pages.
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
markstory committed Nov 28, 2011
1 parent 7e790aa commit cfbc436
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
8 changes: 8 additions & 0 deletions lib/Cake/Controller/CakeErrorController.php
Expand Up @@ -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);
}
}

/**
Expand Down
7 changes: 6 additions & 1 deletion lib/Cake/Controller/Component/RequestHandlerComponent.php
Expand Up @@ -556,7 +556,12 @@ public function renderAs($controller, $type, $options = array()) {
}
$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);
Expand Down
6 changes: 5 additions & 1 deletion lib/Cake/Error/ExceptionRenderer.php
Expand Up @@ -181,6 +181,7 @@ protected function _cakeError(CakeException $error) {
'url' => h($url),
'name' => $error->getMessage(),
'error' => $error,
'serialize' => array('code', 'error', 'name', 'url')
));
try {
$this->controller->set($error->getAttributes());
Expand Down Expand Up @@ -208,7 +209,8 @@ public function error400($error) {
$this->controller->set(array(
'name' => $message,
'url' => h($url),
'error' => $error,
'error' => $error,
'serialize' => array('error', 'name', 'url')
));
$this->_outputMessage('error400');
}
Expand All @@ -231,6 +233,7 @@ public function error500($error) {
'name' => $message,
'message' => h($url),
'error' => $error,
'serialize' => array('error', 'name', 'url')
));
$this->_outputMessage('error500');
}
Expand All @@ -250,6 +253,7 @@ public function pdoError(PDOException $error) {
'url' => h($url),
'name' => $error->getMessage(),
'error' => $error,
'serialize' => array('code', 'error', 'name', 'url')
));
try {
$this->_outputMessage($this->template);
Expand Down
27 changes: 27 additions & 0 deletions lib/Cake/View/JsonView.php
@@ -0,0 +1,27 @@
<?php

class JsonView extends View {

/**
* Render a JSON view.
*
* Uses the special 'serialize' parameter to convert a set of
* view variables into a JSON response. Makes generating simple
* JSON responses very easy. You can omit the 'serialize' parameter,
* and use a normal view + layout as well.
*
* @param string $view The view being rendered.
* @param string $layout The layout being rendered.
* @return The rendered view.
*/
public function render($view = null, $layout = null) {
if (isset($this->viewVars['serialize'])) {
$vars = array_intersect_key(
$this->viewVars,
array_flip($this->viewVars['serialize'])
);
return json_encode($vars);
}
return parent::render($view, $layout);
}
}

0 comments on commit cfbc436

Please sign in to comment.