Skip to content

Instantly share code, notes, and snippets.

@hugodias
Created March 20, 2014 12:42
Show Gist options
  • Save hugodias/9662954 to your computer and use it in GitHub Desktop.
Save hugodias/9662954 to your computer and use it in GitHub Desktop.
Error handling in CakePHP 2.0+
Exception handling in cakephp 2.0+
Now, I discribe step by step how to handle error 400,403,404,405,501 in cakephp2.0+
Step-1 : Update /app/Config/core.php
open /app/Config/core.php, and find the following:
Configure::write('Exception', array(
'handler' => 'ErrorHandler::handleException',
'renderer' => 'ExceptionRenderer',
'log' => true
));
and Replace with following:
Configure::write('Exception', array(
'handler' => 'ErrorHandler::handleException',
'renderer' => 'AppExceptionRenderer',
'log' => true
));
Step-2 : Create following file if not exists
1. /app/View/Errors/error400.ctp
2. /app/View/Errors/error403.ctp
3. /app/View/Errors/error404.ctp
4. /app/View/Errors/error405.ctp
5. /app/View/Errors/error500.ctp
6. /app/View/Errors/error501.ctp
in above file , you can put your custom error design.
Step-3 : Create /app/Lib/Error/AppExceptionRenderer.php
App::uses('ExceptionRenderer', 'Error');
class AppExceptionRenderer extends ExceptionRenderer {
/*-------------------------------------------------------------------------*/
/* -- Exceptions
/*-------------------------------------------------------------------------*/
public function notFound($error) {
$this->controller->beforeFilter();
$this->controller->set('title_for_layout', 'Not Found');
$this->controller->render('/Errors/error404');
$this->controller->response->send();
}
public function badRequest($error) {
$this->controller->beforeFilter();
$this->controller->set('title_for_layout', 'Not Found');
$this->controller->render('/Errors/error400');
$this->controller->response->send();
}
public function forbidden($error) {
$this->controller->beforeFilter();
$this->controller->set('title_for_layout', 'Not Found');
$this->controller->render('/Errors/error403');
$this->controller->response->send();
}
public function methodNotAllowed($error) {
$this->controller->beforeFilter();
$this->controller->set('title_for_layout', 'Not Found');
$this->controller->render('/Errors/error405');
$this->controller->response->send();
}
public function internalError($error) {
$this->controller->beforeFilter();
$this->controller->set('title_for_layout', 'Not Found');
$this->controller->render('/Errors/error500');
$this->controller->response->send();
}
public function notImplemented($error) {
$this->controller->beforeFilter();
$this->controller->set('title_for_layout', 'Not Found');
$this->controller->render('/Errors/error501');
$this->controller->response->send();
}
/*-------------------------------------------------------------------------*/
/* -- Other
/*-------------------------------------------------------------------------*/
public function missingController($error) {
$this->notFound($error);
}
public function missingAction($error) {
$this->notFound($error);
}
public function missingView($error) {
$this->notFound($error);
}
public function missingLayout($error) {
$this->internalError($error);
}
public function missingHelper($error) {
$this->internalError($error);
}
public function missingBehavior($error) {
$this->internalError($error);
}
public function missingComponent($error) {
$this->internalError($error);
}
public function missingTask($error) {
$this->internalError($error);
}
public function missingShell($error) {
$this->internalError($error);
}
public function missingShellMethod($error) {
$this->internalError($error);
}
public function missingDatabase($error) {
$this->internalError($error);
}
public function missingConnection($error) {
$this->internalError($error);
}
public function missingTable($error) {
$this->internalError($error);
}
public function privateAction($error) {
$this->internalError($error);
}
}
-- in above function,
set method:
We need to do is set our title_for_layout variable so it’s passed to the view
render method :
The first params is the view you want to render and
The second params is the layout to render it in (second is optional).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment