Alright, I've figured out a solution that is working for me:
class AppExceptionHandler {
public static function handleException($error) {
$request = new CakeRequest();
$request_type = AppExceptionHandler::parseExtension($request-
>here);
switch ($request_type) {
case 'json':
AppExceptionHandler::renderJson($error);
break;
case 'xml':
AppExceptionHandler::renderXml($error);
break;
case 'html':
default:
AppExceptionHandler::renderHtml($error);
break;
}
}
public static function parseExtension($url) {
$ext = null;
if (preg_match('/\.[0-9a-zA-Z]*$/', $url, $match) === 1) {
$match = substr($match[0], 1);
if (empty(self::$_validExtensions)) {
$url = substr($url, 0, strpos($url, '.' . $match));
$ext = $match;
} else {
foreach (self::$_validExtensions as $name) {
if (strcasecmp($name, $match) === 0) {
$url = substr($url, 0, strpos($url, '.' .
$name));
$ext = $match;
break;
}
}
}
}
return $ext;
}
}
I stole the parseExtension method from the Router class in CakePHP. I
may also use the CakeRequest::accepts() method at some point (or maybe
adopt the 2.1 way of doing things when upgrading), but I prefer to use
the extension to make it easier for REST clients to specify what
content type they want returned by the API.
It's a hack, so if there is a better solution I would like to know.
On Jan 5, 2:59 pm, Neil Goodman <[email protected]> wrote:
> Thanks. That is getting me closer. However the request object doesn't
> contain the same information as it does in the controller. How can I
> load the $request->params['ext'] value that is present with the
> RequestHandler and Router::parseExtensions();?
>
> On Jan 4, 7:24 pm, José Lorenzo <[email protected]> wrote:
>
>
>
>
>
>
>
> > Btw, in CakePHP 2.1 the errors will be rendered using the accept content
> > type from the browser! Just what you are trying to do in 2.0.
--
Our newest site for the community: CakePHP Video Tutorials
http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others
with their CakePHP related questions.
To unsubscribe from this group, send email to
[email protected] For more options, visit this group at
http://groups.google.com/group/cake-php