Hello,
First I want to apologize about my poor English, but I'm French.
I am actually learning ZF and I have a problem with the exceptions. I want to use a class in order to show my Exception but it doesn't work :
Bootstrap.php
PHP Code:
<?php
// Rapporte directement toutes les erreurs
error_reporting(E_ALL);
ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
date_default_timezone_set('Europe/Paris');
// Ajoute la library de ZF dans le include_path de PHP
set_include_path('../library' . PATH_SEPARATOR . '../application/models' . PATH_SEPARATOR . get_include_path());
// Permet de charger automatiquement les classes sans avoir besoin de la charger auparavant
require 'Zend/Loader.php';
Zend_Loader::registerAutoload();
// Charge la configuration SQL
$config = new Zend_Config_Ini('../application/config/config.ini', 'general');
$db = Zend_Db::factory($config->db->adapter, $config->db->config->toArray());
Zend_Db_Table::setDefaultAdapter($db);
Zend_Registry::set('dbAdapter', $db);
// Gestion des droits des membres
$auth = Zend_Auth::getInstance();
$acl = new MyLib_Acl($auth);
// Gestion des exceptions
$errorHandler = new Zend_Controller_Plugin_ErrorHandler();
$errorHandler ->setErrorHandlerController('error')
->setErrorHandlerAction('error');
// Gestion de l'instance MVC
$frontController = Zend_Controller_Front::getInstance();
$frontController->setControllerDirectory('../application/controllers')
->registerPlugin($errorHandler)
->registerPlugin(new MyLib_Plugin_Auth($auth, $acl))
->setParam('auth', $auth);
try {
$frontController->dispatch();
} catch (Exception $exception) {
exit($exception->getMessage());
}
And my /controllers/ErrorController.php
PHP Code:
<?php
class ErrorController extends Zend_Controller_Action
{
private $_exception;
private static $errorMessage;
private static $httpCode;
public function preDispatch()
{
$this->_helper->viewRenderer->setNoRender(true);
$this->_exception = $this->_getParam('error_handler');
$this->_response->clearBody();
$this->_response->append('error', null);
switch ($this->_exception->type)
{
case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER :
case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION :
self::$httpCode = 404;
self::$errorMessage = 'Page introuvable';
break;
case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_OTHER :
switch (get_class($this->_exception->exception))
{
case 'Zend_View_Exception' :
self::$httpCode = 500;
self::$errorMessage = 'Erreur de traitement d\'une vue';
break;
case 'Zend_Db_Exception' :
self::$httpCode = 503;
self::$errorMessage = 'Erreur de traitement dans la base de données';
break;
case 'Metier_Exception' :
self::$httpCode = 200;
self::$errorMessage = $this->_exception->exception->getMessage();
break;
default :
self::$httpCode = 500;
self::$errorMessage = 'Erreur inconnue';
break;
}
break;
}
}
public function errorAction()
{
$this->getResponse()->setHttpResponseCode(self::$httpCode);
$this->_errorMessage .= sprintf('<p>%s</p>', self::$errorMessage);
}
public function postDispatch()
{
// Sauve l'exception en log
//$exception = $this->_exception->exception;
//$log = new Zend_Log(new Zend_Log_Writer_Stream('/ZendFramework/tmp/logs/Exception.log', 'r+'));
//$log->debug($this->$errorMessage);
$this->getResponse()->appendbody($this->_errorMessage, 'error');
$this->getResponse()->appendbody('<a href="javascript:history.back()>retour</a>', 'error');
}
}
Thanks for your help,
Regards,
Lionel