Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Exception Handling in Zend Framework

Prepared by: Er. Vishanta Rayamajhi, Int’l IT Expert, UNDP

www/index.php

<?php

error_reporting(E_ALL|E_STRICT);
ini_set('display_errors', true);
date_default_timezone_set('Asia/Thimphu');

require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Controller_Front');

// setup controller
$frontController = Zend_Controller_Front::getInstance();
$frontController->setControllerDirectory('../application/controllers');

// run!
$frontController->dispatch();

Localization of E-Governance Project (DIT, UNDP) vishanta.rayamajhi@gmail.com 1|Page


application/controllers/IndexController.php

<?php

class IndexController extends Zend_Controller_Action


{
public function indexAction()
{
$this->view->assign('title', 'Zend Framework - MVC paradigm');
$this->view->assign('content', 'Exception Handling in Zend Framework');
$this->view->assign('footer', 'Er. Vishanta Rayamajhi, Intl IT Consultant, UNDP');
}
}

application/controllers/ErrorController.php

<?php

class ErrorController extends Zend_Controller_Action


{
public function errorAction()
{
$errors = $this->_getParam('error_handler');
switch ($errors->type) {
case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
// 404 error -- controller or action not found
$this->getResponse()->setRawHeader('HTTP/1.1 404 Not Found');
$this->view->errorType = 404;
$this->view->exception = $errors->exception;
break;
default:
throw $errors->exception;
break;
}
}
}

application/views/scripts/index/index.phtml

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"


"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title><?php echo $this->escape($this->title);?></title>
</head>
<body>
<h1><?php echo $this->escape($this->title);?></h1>
<hr>
<div id="content">
<?php echo $this->escape($this->content);?>
</div>
<br><br><br><br>
<i><?php echo $this->escape($this->footer);?></i>
</body>
</html>
Localization of E-Governance Project (DIT, UNDP) vishanta.rayamajhi@gmail.com 2|Page
application/views/scripts/error/error.phtml

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"


"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>Error</title>
</head>
<body>
<h1>Error</h1>
<p>An unexpected error has occurred.</p>
<?php
$msg = $this->exception->getMessage();
$trace = $this->exception->getTraceAsString();
?>
<hr />
<div>Error: <?php echo $msg ?></div>
<pre><?php echo $trace ?></pre>
</body>
</html>

Output in Browser:

Localization of E-Governance Project (DIT, UNDP) vishanta.rayamajhi@gmail.com 3|Page


Exception caught accessing non-existent “action”

Exception caught accessing non-existent “controller”

Localization of E-Governance Project (DIT, UNDP) vishanta.rayamajhi@gmail.com 4|Page

You might also like