适用于Zend Framework应用程序的有用的错误控制器类
任何应用程序的一项有用功能是报告发生的任何错误。ZendFramework带有一个不错的错误控制器系统,您可以通过创建ErrorController类来激活它。
以下是我使用的ErrorController类。它检测出发生了哪种类型的错误,并向用户显示一条消息。它还会将有关错误的详细报告通过电子邮件发送给服务器管理员。
class ErrorController extends Zend_Controller_Action
{
public function errorAction()
{
//确保使用默认的视图后缀,以便我们始终返回良好
//内容
$this->_helper->viewRenderer->setViewSuffix('phtml');
//从请求中获取错误对象
$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错误-找不到控制器或动作
$this->getResponse()->setHttpResponseCode(404);
$this->view->message = 'Page not found';
$this->view->code = 404;
if ($errors->type == Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER) {
$this->view->info = sprintf(
'Unable to find controller "%s" in module "%s"',
$errors->request->getControllerName(),
$errors->request->getModuleName()
);
}
if ($errors->type == Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION) {
$this->view->info = sprintf(
'Unable to find action "%s" in controller "%s" in module "%s"',
$errors->request->getActionName(),
$errors->request->getControllerName(),
$errors->request->getModuleName()
);
}
break;
case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
default:
//应用程序错误
$this->getResponse()->setHttpResponseCode(500);
$this->view->message = 'Application error';
$this->view->code = 500;
$this->view->info = $errors->exception;
break;
}
//发送邮件让某人知道有问题!
$config = Zend_Registry::get('configuration');
$tr = new Zend_Mail_Transport_Smtp($config->smtp);
Zend_Mail::setDefaultTransport($tr);
$mail = new Zend_Mail();
$emailBody = "An error occurred in the system. The error message contained the following output:\n\n";
$emailBody .= $this->view->message." (".$this->view->code.")\n\n";
$emailBody .= "Zend Error Type: ".$errors->type."\n\n";
$emailBody .= "REQUEST_URI: ".$_SERVER['REQUEST_URI']."\n\n";
if ( isset($_SERVER['HTTP_REFERER']) ) {
$emailBody .= "HTTP_REFERER: ".$_SERVER['HTTP_REFERER']."\n\n";
}
$emailBody .= "Stack trace: \n\n". $errors->exception->getTraceAsString()."\n\n";
//找到用户的罪魁祸首!
$username = Zend_Auth::getInstance()->getIdentity();
$emailBody .= "This error was created by ".$username.".";
$mail->setBodyText($emailBody);
$mail->setFrom('[email protected]', '');
$mail->addTo($config->adminEmail, $config->adminName);
$mail->setSubject('An Error Occured');
//电子邮件
$mail->send();
$this->view->title = 'Error!';
$this->view->heading = 'Error!';
//将环境传递给视图脚本,以便我们可以有条件地
//显示更多/更少信息
$this->view->env = $this->getInvokeArg('env');
//将实际的异常对象传递给视图
$this->view->exception = $errors->exception;
//将请求传递给视图
$this->view->request = $errors->request;
}
}此错误控制器依赖于配置文件中已设置的某些选项。将以下选项添加到您的配置文件中,以使这些选项起作用。
smtp = my.smtpserver.com adminEmail = [email protected] adminName = 'Your name'
要启用此throwExceptions()功能,只需调用Zend_Controller_Front的函数并将其值传递为false。这将导致框架寻找错误控制器,而不是尝试打印出东西。
最后,您需要添加一个视图,以便您的错误将得到很好的显示。如果环境设置为“测试”,它将显示更多。
Error has happened. Inside this application. Sorry about that.
=$this->message ?> (=$this->code ?>)
if( $this->env == 'test' ) { if ( isset($this->info ) ) { ?> if ( 404 == $this->code ) { ?>Reason: = $this->info ?>
} elseif (500 == $this->code) { ?>Bad server, naughty server!
No donut for you!
Exception information:
Message: = $this->info->getMessage() ?>
Stack trace:
= $this->info->getTraceAsString() ?>} ?> } ?> } ?>