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.
This commit is contained in:
mark_story 2011-09-16 21:52:12 -04:00
parent 7e790aa6cb
commit cfbc43671e
4 changed files with 46 additions and 2 deletions

View file

@ -42,9 +42,17 @@ class CakeErrorController extends AppController {
*/ */
public function __construct($request = null, $response = null) { public function __construct($request = null, $response = null) {
parent::__construct($request, $response); parent::__construct($request, $response);
if (count(Router::extensions())) {
$this->components[] = 'RequestHandler';
}
$this->constructClasses(); $this->constructClasses();
$this->Components->trigger('initialize', array(&$this)); $this->Components->trigger('initialize', array(&$this));
$this->_set(array('cacheAction' => false, 'viewPath' => 'Errors')); $this->_set(array('cacheAction' => false, 'viewPath' => 'Errors'));
if (isset($this->RequestHandler)) {
$this->RequestHandler->startup($this);
}
} }
/** /**

View file

@ -556,7 +556,12 @@ class RequestHandlerComponent extends Component {
} }
$controller->ext = '.ctp'; $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; $controller->viewPath .= DS . $type;
} else { } else {
$remove = preg_replace("/([\/\\\\]{$this->_renderType})$/", DS . $type, $controller->viewPath); $remove = preg_replace("/([\/\\\\]{$this->_renderType})$/", DS . $type, $controller->viewPath);

View file

@ -181,6 +181,7 @@ class ExceptionRenderer {
'url' => h($url), 'url' => h($url),
'name' => $error->getMessage(), 'name' => $error->getMessage(),
'error' => $error, 'error' => $error,
'serialize' => array('code', 'error', 'name', 'url')
)); ));
try { try {
$this->controller->set($error->getAttributes()); $this->controller->set($error->getAttributes());
@ -209,6 +210,7 @@ class ExceptionRenderer {
'name' => $message, 'name' => $message,
'url' => h($url), 'url' => h($url),
'error' => $error, 'error' => $error,
'serialize' => array('error', 'name', 'url')
)); ));
$this->_outputMessage('error400'); $this->_outputMessage('error400');
} }
@ -231,6 +233,7 @@ class ExceptionRenderer {
'name' => $message, 'name' => $message,
'message' => h($url), 'message' => h($url),
'error' => $error, 'error' => $error,
'serialize' => array('error', 'name', 'url')
)); ));
$this->_outputMessage('error500'); $this->_outputMessage('error500');
} }
@ -250,6 +253,7 @@ class ExceptionRenderer {
'url' => h($url), 'url' => h($url),
'name' => $error->getMessage(), 'name' => $error->getMessage(),
'error' => $error, 'error' => $error,
'serialize' => array('code', 'error', 'name', 'url')
)); ));
try { try {
$this->_outputMessage($this->template); $this->_outputMessage($this->template);

View file

@ -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);
}
}